var anarray = [0,1,2];
var anotherone = [3,4,5];
var more = [10.3,5.34,99];
For testing purposes, I’d like to re-define those arrays by reading new ones from a text file. If I were starting from scratch, I could do this by structuring my script differently, but I’m not. so lets say our text file has
anarray = [5,6,7];
anotherone = [8,9,7];
more = [100,102,103];
Now, I can use the File object in JS to read the lines, and for each line, separate these with some split() and maybe a bit of regexp, and end up with a variable that contains the array name, and the array itself. For example the first line could give me:
thename = "anarray";
tmparray = [5,6,7];
but I’m at a loss of how to actually populate the array "anarray" with the contents of tmparray based on the symbol "anarray".
I can do it if I’m strict in my textfile and setup a bunch of ugly case statements based on line number, but I’d rather be able to do this more elegantly.
As long as you are sure the textfile doesn’t contain anything malicious or problematic (and you could check this beforehand with regexp) you can always just use eval(). I wouldn’t normally recommend using it because it is not the safest method, especially if you need it to be adaptable or used by the general public but it should do the job in this situation.
sweet, that’ll work. It’s for "internal use only". thanks
C74 RSS Feed | © Copyright Cycling '74