compare table while iterating

Hello,
While I iterate throw a table of data I have to detect some new event (an increment column). On each new event I create a sub table and I have to verify whether or not the actual sub table is the same as the previous one.
below is a simulation of my code where it works fine (whereas with my code I see some strange behavior depending if the current table is reloaded from the disk (with loadTable) or not.... and I don't get it )
any suggestion ?
I saw that equals()is present for Object like Table but I don't manage to make it work.

String data[] = {"a", "b", "c", "d", "e", "f"};
Table temp; 
String titles[] = {"first", "second"};
void setup() {
  temp = new Table(); 
  temp.setColumnTitles(titles);

  int tableNmb = 0;  
  for (int i=0; i< 30; i++) {

    if (i%5 == 0) {

      //delete temp table
      temp.clearRows();

      //add data to table and save
      temp.addRow();
      temp.setString(0, "first", "a");
      temp.setString(0, "second", "b");
      temp.addRow();
      temp.setString(1, "first", "c");
      temp.setString(1, "second", data[int(random(3))]);
      temp.sort("second");
      saveTable(temp, "table"+tableNmb+".csv") ;

      //compare with last table
      if (tableNmb>0) {
        boolean equality = false; 
        Table prevTable = new Table();
        prevTable = loadTable("table"+(tableNmb-1)+".csv", "header");

        equality =  thisTableAreEqual(prevTable, temp) ;
        println("compare table "+tableNmb+" with previous, result : "+equality);
      }

      tableNmb+=1;
    }
  }
  exit();
}


boolean thisTableAreEqual(Table A, Table B) {
  for (int i=0; i< A.getRowCount(); i++) {
    TableRow rowA = A.getRow(i) ; 
    TableRow rowB = B.getRow(i) ;

    for (int j=0; j< 2; j++) { 
      if ( rowA.getString(j) == null) { // if the cell is null
        if (rowB.getString(j) != null) return false;
      } else if ( ! rowA.getString(j).equals(rowB.getString(j))) return false; 
      //if (!isEqual) temp+="line "+i+" column "+j;
    }
  }
  return true;
}

thx

Tagged:
Sign In or Register to comment.