When I try to import a CSV file using JavaScript, the dataset imports successfully into my program. However, I keep encountering the error csv[i] not defined (line 57). I attempted to add var i = 0; just before that line, but the error persists. Any suggestions on what I can do to resolve this issue?
//
// =============================================================
// CSV to coll - custom JavaScript code
// =============================================================
//
//
//
/***************************************************************
Important Information:
This JS object serves to import a CSV file and convert it for use within a coll.
It also calculates two spreads between open/close and high/low, while providing the minimum and maximum values of each column.
These minima and maxima can be utilized to set the range of the zmap object down the chain.
****************************************************************/
// =============================================================
// Inlets and Outlets
// =============================================================
outlets = 6;
// =============================================================
// Functions Begin Here
// =============================================================
/***************************************************************
This function handles the importing of the CSV file. It skips the first line (header row) and converts lines to strings.
****************************************************************/
function importfromfile(filename)
{
var f = new File(filename);
var csv = [];
var x = 0;
if (f.open) {
var str = f.readline(); //Skips first line.
while (f.position < f.eof) {
var str = f.readline();
csv.push(str);
}
f.close();
} else {
error("couldn't find the file ("+ filename +")\n");
}
/***************************************************************
1) The CSV data is read into the coll/cellblock
2) Spreads between high-low and open-close are calculated and sent out to the coll/cellblock
3) The maximum value of each column is determined and transmitted to outlet 1
****************************************************************/
var maxtimestamp=0;
var maxdatavalue=0;
for (var i=0; i<=csv.length; i++) {
var a = csv[i].split(",");
var timestamp = parseFloat(a[0]);
var datavalue = parseFloat(a[1]);
maxtimestamp=(timestamp>maxtimestamp)? timestamp : maxtimestamp; // open overwrites the max if greater
maxdatavalue=(datavalue>maxdatavalue)? datavalue : maxdatavalue; // open overwrites the max if greater
outlet(0, x++, timestamp, datavalue);
outlet(1, maxtimestamp, maxdatavalue);
outlet(4, csv.length);
}
// The minimum value of each column is determined and sent to outlet 2
// A bang to outlet 3 ensures the coll is referenced in the cellblock
var mintimestamp=Infinity;
var mindatavalue=0;
for (var i=0; i<=csv.length; i++) {
var a = csv[i].split(",");
var timestamp = parseFloat(a[0]);
var datavalue = parseFloat(a[1]);
mintimestamp=(timestamp<mintimestamp)? timestamp : mintimestamp; // open overwrites the min if less
datavalue=(datavalue<mindatavalue)? datavalue : mindatavalue; // open overwrites the min if less
outlet(2, mintimestamp, mindatavalue);
outlet(3, mintimestamp);
outlet(4, "bang");
}
}