Loading...
Logo
Processing Forum

asymmetrical 2D array

in General Discussion  •  Other  •  9 months ago  
Hello,

I wonder if I could get an index number on a 2D array I am working with.
The array is asymmetrical, and I would like to enumerate how many items there are (not nulls)

I'm bringing in a text file with comma separated strings, the 2D Array is from a list like this;

fact, more fact, some more facts
text, more text, yet more text, too much text
bits, more bits

Would I use a nested for loop and count them somehow?

Replies(9)




double for-loop is correct
use length to determine the length of that array.
see
http://www.processing.org/reference/Array.html

no problem


e.g.

(I realize men is not really a 2D array but to show the trick)

Copy code
  1. //
  2. String[] men = { 
  3.   "Chernenko ] Andropov ] Brezhnev", "C ] Av", "Chernenko ] Andropov ] Brezhnev ] 33 ] poep"
  4. };
  5. for (int i=0; i<men.length; i++) {
  6.   String[] list = split(men[i], " ] ");
  7.   //
  8.   print(list.length+": ");
  9.   // list.length is different every time:
  10.   for (int j=0; j<list.length; j++) {
  11.     print (list [j] + "  --  ");
  12.   } // for j
  13.   //
  14.   println ();
  15.   println ("------------------------");
  16.   //
  17. } // for i


gives you



3: Chernenko  --  Andropov  --  Brezhnev  -- 
------------------------
2: C  --  Av  -- 
------------------------
5: Chernenko  --  Andropov  --  Brezhnev  --  33  --  poep  -- 
------------------------


Thanks Chrisir,

And over the holidays as well :)

I'll give  that a try, cheers.
Hi,

It seems easy seeing the values printed in the rows above, but I am having trouble conceptualizing it in this asymmetrical  2D String array I have. 

I'd like to have the number of total strings in the array. Just one number saying the number of total strings.

I see the values coming through per row above, but I can't seem to make the leap to adding these up, I cant say why.

I've been looking at the code below on the web, and I believe I should be doing a sum of an array of int row values, but cannot get it.

I can show you my own code trying this, but its horrible, ...any guidance? 
Copy code
  1. //for importing grid files into a 2d array
  2. //by che-wei wang

  3. String rows[] = loadStrings("List.txt");
  4. String [][] grid;
  5. int cols=0;

  6. //calculate max width of grid file
  7. for (int i=0; i < rows.length; i++) {    //rows
  8.   String [] chars=split(rows[i],',');    //strip out to rows [i]
  9.   if (chars.length>cols){             //if greater than 0
  10.     cols=chars.length;                //set width to 
  11.   }
  12. }

  13. //create grid array based on # of rows and columns in grid file
  14. grid = new String [rows.length][cols];

  15. //parse values into 2d array
  16. for (int i=0; i < rows.length; i++) {
  17.   String [] temp = new String [rows.length];
  18.   temp= split(rows[i], ',');
  19.   for (int j=0; j < temp.length; j++){
  20.    grid[i][j]=temp[j];
  21.   }
  22. }

Re: [WatchList] asymmetrical 2D array

9 months ago   - via EMail-to-forum

I like your Code. It looks good.

Can't you say
Copy code
  1. counter++;
After line 25 / second last line
to count the Strings??

Also you can loop the outer array and add up the length of the inner array.
like this...



Copy code
  1. //for importing grid files into a 2d array
  2. //by che-wei wang
  3. String rows[] = loadStrings("List.txt");
  4. String [][] grid;
  5. int cols=0;
  6. //calculate max width of grid file
  7. for (int i=0; i < rows.length; i++) {    //rows
  8.   String [] chars=split(rows[i], ',');    //strip out to rows [i]
  9.   if (chars.length>cols) {             //if greater than 0
  10.     cols=chars.length;                //set width to
  11.   }
  12. }
  13. //create grid array based on # of rows and columns in grid file
  14. grid = new String [rows.length][cols];
  15. //
  16. int count=0;
  17. //parse values into 2d array
  18. for (int i=0; i < rows.length; i++) {
  19.   String [] temp = new String [rows.length];
  20.   temp= split(rows[i], ',');
  21.   for (int j=0; j < temp.length; j++) {
  22.     grid[i][j]=temp[j];
  23.     count++;
  24.   }
  25. } // for
  26. println ("Strings in total: " + count );




for


fact, more fact, some more facts
text, more text, yet more text, too much text
bits, more bits





here ..............




Copy code
  1. //for importing grid files into a 2d array
  2. //by che-wei wang
  3. String rows[];
  4. String [][] grid;
  5. int cols=0;
  6. //
  7. void setup() {
  8.   size(800, 800);
  9.   rows = loadStrings(dataPath("List.txt"));
  10.   //calculate max width of grid file
  11.   for (int i=0; i < rows.length; i++) {    //rows
  12.     String [] chars=split(rows[i], ',');    //strip out to rows [i]
  13.     if (chars.length>cols) {             //if greater than 0
  14.       cols=chars.length;                //set width to
  15.     }
  16.   }
  17.   //create grid array based on # of rows and columns in grid file
  18.   grid = new String [rows.length][cols];
  19.   //
  20.   int count=0;
  21.   //parse values into 2d array
  22.   for (int i=0; i < rows.length; i++) {
  23.     String [] temp = new String [rows.length];
  24.     temp= split(rows[i], ',');
  25.     for (int j=0; j < temp.length; j++) {
  26.       grid[i][j]=temp[j];
  27.       count++;
  28.     }
  29.   } // for
  30.   println ("Strings in total: " + count );
  31. }
  32. //
  33. void draw() {
  34.   background (0);
  35.   for (int i=0; i < grid.length; i++) {
  36.     for (int j=0; j < grid[i].length; j++) {
  37.       if (grid[i][j]!=null)
  38.         text (grid[i][j], j*130+70, i *  30 + 40);
  39.     }
  40.   }
  41. }

nice one :)

Thank you!