Skip to main content
ChristianK-Fr
Inspiring
August 18, 2022
Question

Help with script I'm testing and I 'd like to understand issue

  • August 18, 2022
  • 3 replies
  • 334 views

Hi, 

I write a little script and have issue can't find solution :

The script read a csv file with comma separate, and I try to store individual values in a array but I have undefined value in my array. Here is my script : 

var ref = File.openDialog("Please open csv file separate with comma :");
var cur = [];  // my Array for import data
ref.open("r");
var temp = ref.readln();
var cur2 = temp.split(","); // Splitting the file 
alert("Have found  : "+ cur2.length+" datas");
alert("Here is what I found : "+cur2);
while (  !ref.eof ) { // Here I try to put datas on array but it doesn't work...
cur[ cur.length ] = ref.readln();
  } 
ref.close();
for (var i = 0; i < cur2.length; i++){ // Loop to see what's happen....
    result = cur[i]
alert("cur = ["+i+"] have this :" + result +" ...and in var cur2 have this : " +cur2[i]);
    };   
alert("That's all !");

Thanks in advance,

Christian

This topic has been closed for replies.

3 replies

Legend
August 19, 2022

In your code, cur.length is zero. So you are trying to read lines into a single item in your array. This isn't going to work.

Have try-catch statements in your code to help you figure out which lines are faulting.

willcampbell7
Legend
August 19, 2022

There are many ways your code to parse CSV can fail. Simply splitting on commas doesn't account for cells that might contain commas. Then there are escaped quotation marks to consider. No need to re-invent the wheel. Here is my code for parsing CSV. Pass to the function the text contents (a string) of a CSV file. The function returns a two-dimensional array, [[String row, String column]]. It will include the head column which I strip off with array shift on the result, leaving only the data rows.

function parseCsv(data, delimiter) {
    // data: String = contents of a CSV file
    // delimiter: character that separates columns
    //            undefined defaults to comma
    // Returns: Array [[String row, String column]]
    var c = ""; // Character at index.
    var d = delimiter || ","; // Default to comma.
    var endIndex = data.length;
    var index = 0;
    var maxIndex = endIndex - 1;
    var q = false; // "Are we in quotes?"
    var result = []; // Array of rows (array of column arrays).
    var row = []; // Array of columns.
    var v = ""; // Column value.
    while (index < endIndex) {
        c = data[index];
        if (q) { // In quotes.
            if (c == "\"") {
                // Found quote; look ahead for another.
                if (index < maxIndex && data[index + 1] == "\"") {
                    // Found another quote means escaped.
                    // Increment and add to column value.
                    index++;
                    v += c;
                } else {
                    // Next character not a quote; last quote not escaped.
                    q = !q; // Toggle "Are we in quotes?"
                }
            } else {
                // Add character to column value.
                v += c;
            }
        } else { // Not in quotes.
            if (c == "\"") {
                // Found quote.
                q = !q; // Toggle "Are we in quotes?"
            } else if (c == "\n" || c == "\r") {
                // Reached end of line.
                // Test for CRLF.
                if (c == "\r" && index < maxIndex) {
                    if (data[index + 1] == "\n") {
                        // Skip trailing newline.
                        index++;
                    }
                }
                // Column and row complete.
                row.push(v);
                v = "";
                // Add row to result if first row or length matches first row.
                if (result.length == 0 || row.length == result[0].length) {
                    result.push(row);
                }
                row = [];
            } else if (c == d) {
                // Found comma; column complete.
                row.push(v);
                v = "";
            } else {
                // Add character to column value.
                v += c;
            }
        }
        if (index == maxIndex) {
            // Reached end of data; flush.
            if (v.length || c == d) {
                row.push(v);
            }
            // Add row to result if length matches first row.
            if (row.length == result[0].length) {
                result.push(row);
            }
            break;
        }
        index++;
    }
    return result;
}

 

William Campbell
Legend
August 18, 2022

I don't fully understand your code. In cur2 you want to put the headers of the table, and in cur all the contents of the file except for the first line?

 

var ref = File.openDialog("Please open csv file separate with comma :");
var cur = [];  // my Array for import data
ref.open("r");
var temp = ref.readln();
var cur2 = temp.split(","); // Splitting the file 
alert("Have found  : " + cur2.length + " datas");
alert("Here is what I found : " + cur2);
while (!ref.eof) { // Here I try to put datas on array but it doesn't work...
    cur.push(ref.readln());
}
ref.close();
for (var i = 0; i < cur.length; i++) { // Loop to see what's happen....
    result = cur[i]
    alert("cur = [" + i + "] have this :" + result);
};
alert("That's all !");

 

 

ChristianK-Fr
Inspiring
August 19, 2022

Hi, thks for answer, my intend is to read a csv file with comma separate, and store information in a array. Have a script works with csv file with lines, but it doesn't work when I used csv file with comma separate. So I try to modify script but it doesn't work..... Here is the script works for csv with lines  : 

 

var ref = File.openDialog("Open csv file");
var cur = [];
ref.open("r");
while (  !ref.eof ) {  
cur[ cur.length ] = ref.readln();
  } 
ref.close();
for (var i = 0; i < cur.length; i++){
    data = cur[i]
alert(i+"==="+data);
    };   

 

Regards, 

Christian