We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › silly/simple but...Table
Page Index Toggle Pages: 1
silly/simple ? but...Table (Read 1455 times)
silly/simple ? but...Table
Feb 21st, 2009, 5:19pm
 
this example is from fry's "Visualizing Data."  for the first few runs, it did work; now it doesn't, specifying that "Cannot find a class or type named "Table." Below is the pasted code.  i have tried to run multiple versions (i.e. "Table.pde" in both the data and sketch folder, listing the class "Table" following the original sketch code, etc.)  I know this is so basic but can anyone offer some insight so I can bang my head and say "oh, so that's how..."  thanks in advance.

PImage mapImage;
int rowCount;
float dataMin = MAX_FLOAT;
float dataMax = MIN_FLOAT;
Table locationTable;
Table dataTable;

void setup() {
 size(640, 400);
 mapImage = loadImage("map.png");
 // Make a data table from a file that contains
 // the coordinates of each table
 locationTable = new Table("locations.tsv");
 // Read the data table
 dataTable = new Table("random.tsv");
 // The row count will be used a lot, so store it globally.
 rowCount = locationTable.getRowCount();
}

// Find the minimum and the maximum values.
for (int row = 0; row < rowCount; row++) {
 float value = dataTable.getFloat(row, 1);
 if (value > dataMax) {
   dataMin = value;
 }
 if (value < data Mind) {
   dataMin = value;
 }

void draw() {
 background(255);
 image(mapImage, 0, 0);
}

// Drawing attributes for the ellipses
smooth();
fill(192, 0, 0);
noStroke();

// Loop through the rows of the locations file and draw the points.
for (int row = 0; row < rowCount; row++) {
 String abbrev = dataTable.getRowName(row);
 float x = locationTable.getFloat(abbrev);  // column 1
 float y = locationTable.getFloat(abbrev);   // column 2
 drawData(x, y, abbrev);
}

// Map the size of the ellipse to the data value
void drawData(float x, float y, String abbrev) {
 // Get data value for state
 float value = dataTable.getFloat(abbrev, 1);
 float diameter = 0;
 if (value >= 0) {
   diameter = map(value, 0, dataMax, 0, 255);
   fill(#333366. a); // blue
 } else {
   diameter = map(value, 0, dataMin, 0, 255);
   fill(#EC5166, a); // red
 }
 ellipse(x, y, 15, 15);
}
}

class Table {
 String[][] data;
 int rowCount;
 
 
 Table() {
   data = new String[10][10];
 }

 
 Table(String filename) {
   String[] rows = loadStrings(filename);
   data = new String[rows.length][];
   
   for (int i = 0; i < rows.length; i++) {
     if (trim(rows[i]).length() == 0) {
       continue; // skip empty rows
     }
     if (rows[i].startsWith("#")) {
       continue;  // skip comment lines
     }
     
     // split the row on the tabs
     String[] pieces = split(rows[i], TAB);
     // copy to the table array
     data[rowCount] = pieces;
     rowCount++;
     
     // this could be done in one fell swoop via:
     //data[rowCount++] = split(rows[i], TAB);
   }
   // resize the 'data' array as necessary
   data = (String[][]) subset(data, 0, rowCount);
 }


 int getRowCount() {
   return rowCount;
 }
 
 
 // find a row by its name, returns -1 if no row found
 int getRowIndex(String name) {
   for (int i = 0; i < rowCount; i++) {
     if (data[i][0].equals(name)) {
       return i;
     }
   }
   println("No row named '" + name + "' was found");
   return -1;
 }
 
 
 String getRowName(int row) {
   return getString(row, 0);
 }


 String getString(int rowIndex, int column) {
   return data[rowIndex][column];
 }

 
 String getString(String rowName, int column) {
   return getString(getRowIndex(rowName), column);
 }

 
 int getInt(String rowName, int column) {
   return parseInt(getString(rowName, column));
 }

 
 int getInt(int rowIndex, int column) {
   return parseInt(getString(rowIndex, column));
 }

 
 float getFloat(String rowName, int column) {
   return parseFloat(getString(rowName, column));
 }

 
 float getFloat(int rowIndex, int column) {
   return parseFloat(getString(rowIndex, column));
 }
 
 
 void setRowName(int row, String what) {
   data[row][0] = what;
 }


 void setString(int rowIndex, int column, String what) {
   data[rowIndex][column] = what;
 }

 
 void setString(String rowName, int column, String what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = what;
 }

 
 void setInt(int rowIndex, int column, int what) {
   data[rowIndex][column] = str(what);
 }

 
 void setInt(String rowName, int column, int what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }

 
 void setFloat(int rowIndex, int column, float what) {
   data[rowIndex][column] = str(what);
 }


 void setFloat(String rowName, int column, float what) {
   int rowIndex = getRowIndex(rowName);
   data[rowIndex][column] = str(what);
 }
 
 
 // Write this table as a TSV file
 void write(PrintWriter writer) {
   for (int i = 0; i < rowCount; i++) {
     for (int j = 0; j < data[i].length; j++) {
       if (j != 0) {
         writer.print(TAB);
       }
       if (data[i][j] != null) {
         writer.print(data[i][j]);
       }
     }
     writer.println();
   }
   writer.flush();
 }
}
Re: silly/simple ? but...Table
Reply #1 - Feb 21st, 2009, 6:59pm
 
Some of your curly braces aren't lining up--the "Find the minimum and the maximum values." needs to be inside the { and } braces for the setup() function, and similarly, the code that comes after your draw() { ... } needs to be inside draw.

To fix things, I recommend moving the Table class over to its own tab, then carefully go through and match { and } characters so that they pair up properly. (If you put the text cursor after } it will highlight the { that it's paired to.)
Re: silly/simple ? but...Table
Reply #2 - Feb 21st, 2009, 11:12pm
 
thank you.  i appreciate your quick response.  on with the rest of this great book!
Re: silly/simple ? but...Table
Reply #3 - Oct 12th, 2009, 4:02am
 
I have fund the same problem, how do I move the table class to another tab? (As is which lines of code do I move?) I am also a novice processing user! Thanks!
Re: silly/simple ? but...Table
Reply #4 - Oct 12th, 2009, 4:54am
 
Copy (cut) from "class Table {" to the last "}".
Re: silly/simple ? but...Table
Reply #5 - Oct 12th, 2009, 6:52am
 
Perhaps the OP's indentation got lost when the code was pasted here, but this does highlight the usefulness of indenting your code methodically.  E.g. whenever a structure requiring curly braces (class/function declaration, conditions, loops etc.) starts indent the block of code contained within it.  That way it's much easier to spot when closing braces are missing...
Page Index Toggle Pages: 1