Issue
USING THE PROCESSING LIBRARY WITH THEIR Table
OBJECT
I have set the code so that it creates a new row in a Table object def
only when a condition is fulfilled inside a loop, however, it just adds a new row each time the code iterates through the loop:
for (int i=0; i!=data.getRowCount(); i++) { //data is my original table object (37 rows)
if (i%3==0) {
def.addRow(); // def is my result object, which should have about 1/3 of the elements
}
}
print(def.getRowCount()); //will output the same as data.getRowCount()
The problem doesn't seem to be inside the loop, since if you ask it to print()
something, it will do it about 1/3 of data.getRowCount()
times. However, by just running: print(def.getRowCount())
, the result will be the same as data.getRowCount()
when it should be around 1/3.
Full code:
Table data, def;
void setup() {
data=loadTable("binance_data.csv", "header");
def=new Table();
def.addColumn("Year", Table.INT);
def.addColumn("Month", Table.INT);
def.addColumn("Day", Table.INT);
def.addColumn("Hour", Table.INT);
def.addColumn("Minute", Table.INT);
def.addColumn("Second", Table.INT);
def.addColumn("Pair", Table.STRING);
def.addColumn("Side", Table.STRING);
def.addColumn("Price", Table.FLOAT);
def.addColumn("Amount", Table.FLOAT);
for (int i=0; i!=data.getRowCount(); i++) {
if (data.getString(i, "Status").equals("FILLED")) { //this is the actual condition.
String[] blocks=data.getString(i, "Date(UTC)").split(" ");
String[] dates=blocks[0].split("-");
String[] times=blocks[1].split(":");
def.addRow();
def.setInt(i, "Year", int(dates[0]));
def.setInt(i, "Month", int(dates[1]));
def.setInt(i, "Day", int(dates[2]));
def.setInt(i, "Hour", int(times[0]));
def.setInt(i, "Minute", int(times[1]));
def.setInt(i, "Second", int(times[2]));
def.setString(i, "Pair", data.getString(i, "Pair"));
def.setString(i, "Side", data.getString(i, "Side"));
def.setFloat(i, "Price", data.getFloat(i, "Average Price"));
def.setFloat(i, "Amount", data.getFloat(i, "Order Amount"));
}
}
}
Solution
When you set the row data using the i
variable, you're putting the data into that specific numbered row.
So, even if your condition is only triggered once, if i
is 37 at that time then you'll be putting the data into the 37th row (with blank rows filling in before). Remember that i
continues to increment every loop even when the condition is not met.
To fix it, you can get the row that is returned from addRow
and add data using that specific reference, instead of using i
to insert into a numbered row.
TableRow newRow = def.addRow();
newRow.setInt("Year", int(dates[0]));
newRow.setInt("Month", int(dates[1]));
newRow.setInt("Day", int(dates[2]));
// etc...
Answered By - Cadin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.