Does anyone know if you can use loadTable() to generate a Table using a local variable rather than a file as input?
For example if you have CSV-like data stored in a String, or a String[] (per row), or a String[][] (per field), or a StringList (per row)? Really, any of these.
Class Table got some overloaded constructors, like this 1 which gets an InputStream :
/**
* Read the table from a stream. Possible options include:
* <ul>
* <li>csv - parse the table as comma-separated values
* <li>tsv - parse the table as tab-separated values
* <li>newlines - this CSV file contains newlines inside individual cells
* <li>header - this table has a header (title) row
* </ul>
*
* @nowebref
* @param input
* @param options
* @throws IOException
*/
public Table(InputStream input, String options) throws IOException {
init();
parse(input, options);
}
We may try to turn a String into a StringBufferInputStream for it:
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/io/StringBufferInputStream.html
Interesting. Trying to use StringBufferInputStream e.g. in Processing 3.4 stops the sketch on
The type StringBufferInputStream is deprecated
import java.io.StringBufferInputStream;
String tableStr =
"A"+"\t"+"B"+"\t"+"C" + "\n" +
"one"+"\t"+"two"+"\t"+"three" + "\n" +
"foo"+"\t"+"bar"+"\t"+"baz";
Table table;
void setup() {
try {
table = new Table(new StringBufferInputStream(tableStr));
}
catch (IOException err) {
}
print(table);
}
jeremydouglass:
The type StringBufferInputStream is deprecated
Deprecation isn’t an error!
That’s not the signature I referred in my reply. It’s lacking a String as 2nd parameter:
public Table(InputStream input, String options) throws IOException {
True, the message text refers to deprecation – but PDE also stops execution and displays only that message without leaving the try block.
That’s probably it. I’ll try an options string.
In my PDE 3.5.3 it crashes inside the try
block w/ “No extension specified for this Table ” .
I’ve got no deprecation message. But I always had “Continuously check for errors” turned off here!
Great, thanks. Processing 3.4 doesn’t display that error message correctly.
Here is a working demo of a String to Table:
/**
* String to Table
* 2019-07 Processing 3.4
* discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/6
*/
import java.io.StringBufferInputStream;
Table table;
String tableStr =
"A"+"\t"+"B"+"\t"+"C" + "\n" +
"one"+"\t"+"two"+"\t"+"three" + "\n" +
"foo"+"\t"+"bar"+"\t"+"baz";
void setup() {
try {
table = new Table(new StringBufferInputStream(tableStr), "tsv");
}
catch (IOException err) {
}
for (TableRow row : table.rows()) {
for (int col=0; col<row.getColumnCount(); col++) {
print(row.getString(col), " ");
}
println("");
}
exit();
}
output:
A B C
one two three
foo bar baz
2 Likes
glv
November 27, 2022, 11:05pm
8
Hello @jeremydouglass ,
I updated code to remove this in Processing 4.01b:
The type StringBufferInputStream is deprecated
I had some fun with it to stimulate my brain:
/**
* String to Table
* 2019-07 Processing 3.4
* discourse.processing.org/t/use-loadtable-with-variable-like-string-string-string-stringlist/12457/6
*/
/**
* Modified by glv
* 2022-11-27 Processing 4.01
*/
//Reference:
// https://stackoverflow.com/questions/62241/how-to-convert-a-reader-to-inputstream-and-a-writer-to-outputstream
import java.io.ByteArrayInputStream;
Table table;
String tableStr = "";
void setup()
{
size(620, 300);
background(0);
textSize(24);
// Create some data for table
for (int row = 0; row<10; row++)
{
for (int col = 0; col<10; col++)
{
if (col == 0)
tableStr += row + ",";
else if (col == 5)
tableStr += "Really wide text!" + ",";
else
tableStr += col*10.0 + ",";
}
tableStr += "\n";
}
println(tableStr);
try
{
table = new Table(new ByteArrayInputStream(tableStr.getBytes("UTF-8")), "csv");
}
catch (IOException e)
{
}
// Output to console
for (TableRow row : table.rows())
{
for (int col=0; col<row.getColumnCount(); col++)
{
print(row.getString(col), " ");
}
println("");
}
// Outout text to sketch window
for (int row=0; row<table.getRowCount(); row++)
{
int x = 10;
for (int col=0; col<table.getColumnCount(); col++)
{
int y = row;
text(table.getString(row, col), x, 30*y+20); // May have to account for different string lengths!
x += textWidth(table.getString(row, col)) + 10;
}
}
noLoop();
}
:)