Scaling in Table class

edited March 2014 in Using Processing

Hi forum, I had a rather basic question. I am using the in build table class to load this tsv file. Is there a way, I can transform the data which is

    2007     12 15  12
    2007     12 16  29
    2007     12 17  12
    2007     12 18  13
    2007     12 19  5
    2008     1  11  10
    2008     1  12  13
    2008     1  13  25
    2008     1  14  22
    2008     1  15  20

So, is there a way, I can add 12 in the second column when the number in first column is 2008..

       for(TableRow row : table.rows()) {
      int year2 = row.getInt("year");
      int month2 = row.getInt("month");
      int hour2 = row.getInt("hour");
      int count2 = row.getInt("count");

if(year2 == 2008) {
        month2 += 12;
        println(month2);
      }

    }

I tried this..

UPDATE: So i was able to do it, but the problem is, I want to store in an array after increasing its value.. Any idea how can I do it? Now I am at:

    for(TableRow row : table.rows()) {
      int year2 = row.getInt("year");
      int month2 = row.getInt("month");
      int hour2 = row.getInt("hour");
      int count2 = row.getInt("count");

      if(year2 == 2008) {
        month2 += 12;
     //   println(month2);
      }
      for(int i =0; i < table.getRowCount(); i++) {
      NewMonth[i] = month2;                      <---- this  for loop is making all values 12

    }
    } 

Answers

  • edited March 2014

    Take notice that you can modify a Table or a TableRow by replacing get w/ set, like setInt() for example:
    http://processing.org/reference/TableRow_setInt_.html

    And if you prefer to have arrays representing the Table, I've already showed ya in your previous thread:
    http://forum.processing.org/two/discussion/3663/table-class-loadtable

    You can also create a custom class w/ 4 fields to replace the 4 arrays:
    http://wiki.processing.org/w/From_several_arrays_to_classes

    Anyways, here's another example that transfers a Table to a set of arrays again:


    // forum.processing.org/two/discussion/3684/scaling-in-table-class
    
    // Load calendar table:
    final Table calendar = loadTable("calendar.tsv", "header");
    final int len = calendar.getRowCount();
    
    // Instantiate 4 arrays to store each table's columns:
    final short[] years  = new short[len];
    final byte[]  months = new byte[len];
    final byte[]  hours  = new byte[len];
    final byte[]  counts = new byte[len];
    
    // Transfer table to the 4 arrays:
    int idx = 0;
    for (TableRow row: calendar.rows()) {
      years[idx]  = (short) row.getInt(0);
      months[idx] = (byte)  row.getInt(1);
      hours[idx]  = (byte)  row.getInt(2);
      counts[idx] = (byte)  row.getInt(3);
    
      if (years[idx] == 2008)  months[idx] += 12;
    
      ++idx;
    }
    
    // Print out the 4 arrays:
    println("year\tmonth\thour\tcount\n");
    
    for (idx = 0; idx != len; ++idx) {
      print(years[idx]  + "\t");
      print(months[idx] + "\t");
      print(hours[idx]  + "\t");
      print(counts[idx] + "\n");
    }
    
    exit();
    

    year    month   hour    count
    2007    12  15  12
    2007    12  16  29
    2007    12  17  12
    2007    12  18  13
    2007    12  19  05
    2008    01  11  10
    2008    01  12  13
    2008    01  13  25
    2008    01  14  22
    2008    01  15  20
    

    Calendar

Sign In or Register to comment.