[Solved] Strange function behavior

Posted on
  • Hi,

    I am reading a CSV file.

    1587107584119.07397460937,109,0,443,5487,11174
    1587107884119.04345703125,109,0,443,5503,11198
    1587109990123.19384765625,165,0,445,5546,11242
    1587110290123.16333007812,165,0,445,5567,11271
    
    const storage = require("Storage");
    var csvFile = storage.open("activepedom.data.json", "r");
    

    Then I create an array from the entries.

    function getArrayFromCSV(file, index) {
        i = 0;
        array = [];
        now = new Date();
        while ((nextLine = file.readLine())) { //as long as there is a next line
            if(nextLine) {
                dataSplitted = nextLine.split(','); //split line, 0= time, 1=stepsCounted, 2=active, 3=stepsTooShort, 4=stepsTooLong, 5=stepsOutsideTime
                diff = now - dataSplitted[0]; //calculate difference between now and stored time
                if (diff <= history) { //only entries from the last x ms
                    array.push(dataSplitted[index]);
                }
            }
            i++;
        }
        return array;
    }
    

    To get an array with the first column from all lines in a certain timeframe (diff <= history):

    times = getArrayFromCSV(csvFile, 0);
    

    This works fine.
    But as soon as I want to use the function for other columns, I only receive empty arrays ([]).
    Example:

    times = getArrayFromCSV(csvFile, 0);
    steps = getArrayFromCSV(csvFile, 1);
    

    times is filled correctly, steps is an empty array.
    I can call each one individually, but never more than 1.
    What is wrong?

    Thanks

  • The first time you call getArrayFromCSV it reads lines until it gets to the end of the file. The second time around it's already at the end of the file, so there are no more lines to read.

    You can fix this either by reopening the file before each call, or by refactoring your function to pull out all the data you need in one pass.

  • Sat 2020.04.18

    Well Good Morning @Purple-Tentacle. Did a quick hack as I don't have a device I am able to program that supports the 'Storage' module (yet) and works for me.

    Did you try entering csvFile or dump() at the command line?

    >t(1)
    [
      "109",
      "109",
      "165",
      "165"
     ]
    return 109,109,165,165
    
    >t(5)
    [
      "11174",
      "11198",
      "11242",
      "11271"
     ]
    return 11174,11198,11242,11271
    



    Rather than use the debugger, just peppered your function the ol' fashioned way, with my entries remaining far left for comparrison.


    EDIT:
    It appears NebbishHacker posted the exact moment I did, and his analysis confirms what the hack also shows. Entering either command csvFile or dump() would show the contents of memory. Or, write to the WebIDE console as in the hack.


    1 Attachment

  • The second time around it's already at the end of the file, so there are no more lines to read.

    Yes, that was it.
    Many thanks to both of you!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

[Solved] Strange function behavior

Posted by Avatar for Purple-Tentacle @Purple-Tentacle

Actions