Table class loadTable

edited March 2014 in Using Processing

Hi, I have not used this in-built Table class earlier(loadTable), kinda paranoid using this.. Compared to using loadStrings.. Anyway, I had a basic questions- Is there a way, I can make "for (TableRow row : table.rows()) {" in terms of "i"? Quoting from the table example Secondly, in the example given, is it possible to store the results in an array for later use instead of this?

for (TableRow row : table.rows()) {
    int id = row.getInt("id");
    String species = row.getString("species");
    String name = row.getString("name");
}

I wanna store in an array instead of this method..

Answers

  • edited March 2014

    Perhaps declare a helper iterator before the enhanced for loop block? :-??


    // forum.processing.org/two/discussion/3663/table-class-loadtable
    
    final Table lines = loadTable("mammals.csv", "header");
    final int len = lines.getRowCount();
    
    final byte[] ids = new byte[len];
    final String[] species = new String[len], names = new String[len];
    
    int i = 0;
    for ( TableRow row: lines.rows() ) {
      ids[i] = (byte) row.getInt(0);
      species[i] = row.getString(1);
      names[i++] = row.getString(2);
    }
    
    println(ids);
    println();
    
    println(species);
    println();
    
    println(names);
    
    exit();
    

    id,species,name
    0,Capra hircus,Goat
    1,Panthera pardus,Leopard
    2,Equus zebra,Zebra
    

  • Right thanks! Wait, this is the best way to go right? I mean storing all values before in arrays to use other places..(I hope so).. But, is there a way I can convert "TableRow rows: lines.rows()" in terms of "i", like- int i = 0; i < lines.length; i++ or something?

  • edited March 2014 Answer ✓

    Dunno why you'd want a regular for loop for? But here it goes something: (:|

    // forum.processing.org/two/discussion/3663/table-class-loadtable
    
    final Table lines = loadTable("mammals.csv", "header");
    final int len = lines.getRowCount();
    
    final byte[] ids = new byte[len];
    final String[] species = new String[len], names = new String[len];
    
    for (int i = 0; i != len; i++) {
      final TableRow row = lines.getRow(i);
    
      ids[i] = (byte) row.getInt(0);
      species[i] = row.getString(1);
      names[i] = row.getString(2);
    }
    
    println(ids);
    println();
    
    println(species);
    println();
    
    println(names);
    
    exit();
    
  • edited March 2014

    Hey, seems a lil problem; Getting row 856 does not exist; I am not sure my for loop is okay! table = loadTable("entre.tsv", "header"); println(table);

       println(table.getRowCount() + "total");
    
      month = new int[table.getRowCount()];
      year = new int[table.getRowCount()];
    
      count = new int[table.getRowCount()];
      TableRow row = table.getRow(table.getRowCount());
      for (int i = 0; i < table.getRowCount()-1; i++) {  <-- null pointer issue
        month[i] = row.getInt(1);
        hour[i] = row.getInt(2);
        count[i]= row.getInt(3);
      }
    

    ALSO, why can't I use "getIntColumn"; giving me error! Why? http://processing.org/reference/javadoc/core/processing/data/Table.html#getIntColumn(int)

  • The error is probably more in the line 7, where you try to access the row of index row count. Remember that in Java, indexes go from 0 to n-1, so it should be row count - 1. Beside, the loop has no point, as you don't update row. Ie. you read it once, then you loop on it.

    Study again the code from GoToLoop.

    "giving me error!"
    What error? What version of Processing do you have? How do you try to use it?

Sign In or Register to comment.