3

I am importing a csv file using javascript. It imports the dataset in my program but I always get the error csv[i] not defined (line 57). I tried adding vai i = 0; just before that line but it still gives me the error. Any idea what I should add to get rid of this error?

//
// =============================================================
//      CSV to coll - javascript code
// =============================================================
//
//      
//
/***************************************************************
        Notes:
    This JS object is used to import the csv file and convert it for use within a coll. 
    Besides the original data, two spreads (between open and close and between high and low) are calculated
    Furthermore, the minima and maxima of each column are sent out to allow contextualization of the values. 
    These minima and maxima can be used to set the range of the zmap object down the chain. 

    ****************************************************************/
// =============================================================
//                inlets and outlets 
// =============================================================

outlets = 6;

// =============================================================
//                Functions start here 
// =============================================================

/***************************************************************
    this function imports the csv file. The first line (the header row) is skipped and 
    the lines are converted 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 is read into the coll/cellblock 
    2) the spread between high-low and open-close is calculated and set out to the coll/cellblock as well
    3) the maximum of each column is found and sent 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 it greater
    maxdatavalue=(datavalue>maxdatavalue)? datavalue : maxdatavalue; // open overwrites the max if it greater

    outlet(0, x++, timestamp, datavalue); 
    outlet(1, maxtimestamp, maxdatavalue);
    outlet(4, csv.length);
  }
  // the minimum of each column is found and sent out to outlet 2
  // a bang to outlet 3 makes sure that the coll is referred 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 it greater
    datavalue=(datavalue<mindatavalue)? datavalue : mindatavalue; // open overwrites the min if it greater

    outlet(2, mintimestamp, mindatavalue);
    outlet(3, mintimestamp);
    outlet(4, "bang");
  }
}

1 Answer 1

2

This is the problem:

for (var i=0; i<=csv.length; i++) {
// -------------^

Array indexes are 0 through length - 1, not length. Remove the =. Accessing csv[i] when i is csv.length will give you undefined, which will cause an error on the first line of the loop body, where you try to call split on it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.