Loading...
Logo
Processing Forum

Append a string array

in Programming Questions  •  3 months ago  
Hello.

I am using a csv table to load in a string array. I want to add a new row every X minutes, and append this row in the array. However, every time I append, the array is staying the same - it just rewrites the last row. Is this wrong?

rows = loadStrings("data.csv");
newData = append(rows, timestamp);



Replies(4)

append() doesn't modify the existing array, it returns a new array with the additional element.  In your example, the rows variable contains the unmodified array, and newData contains the updated array.
Well, the overall aim is to append the new item, and save the contents of the new array to a file. 
I want this process to be done continuously: i.e. make a new entry, save and recall again in order to append a new item again.

However, I am not sure if using append or Arraylist is the best way to do this. Also, I am not sure if saving and recalling the file all the time is very efficient. Any alternatives that I should consider?

Thanks.
i don't know anything about java memory management but you might be okay just appending the array.  the reason why i hypothesize that despite my lack of memory management knowledge is that if you look at Ben Fry's zipcode example, he's loading 43,000 zip codes into an array and the program loads up quickly and works flawlessly.  you can find it in the examples under visualizing data.  his visualization only loads the array once at the beginning.  now, whether that will hold true for a program that continuously runs, i cannot say.
 
as datguy said, if you want to update the array so you keep appending numbers continuously, you'll have to do this:

rows = (rows, timestamp);
There is no reason to re-read the file every time, but you can keep updating the file for future reference.

So, do this once (possibly in setup):

       rows = loadStrings("data.csv");

Then, do this periodically:
       rows = append(rows, timestamp);
      saveStrings("data.csv", rows);

This will keep the collection updated in memory and it will keep the file updated.  One other thought - depending on the complexity of the data you are saving, you might consider using loadTable and saveTable.  They are specifically designed for csv files.