create Array dynamically?

Jun 9, 2006 at 2:45pm

create Array dynamically?

Is it possible to create a javascript array with an arbitrary name on the fly? I don’t quite see how I can do this.

the goal is to create a function that takes a path to a folder, iterates through the folder and 3-5 layers of subfolders, and puts the paths of all files in those folders into an array. Ideally, this array could be made on the fly, so I don’t have to preallocate a LOT of arrays at the start of my js code. That would be messy, not to mention limiting!

to show you how much I don’t understand, here is a code snippet that of course doesn’t work:

function newone(v,a)
{
v = new Array();
v[0]=a;
}

the message “newone jones 2″ just adds the value 2 to the array “v”.

peter

#26354
Jun 9, 2006 at 3:01pm

maybe you could do something with a “2 dimensional” array.

v[v.length + 1] = new Array();

sort of thing…..

Then every time you need to create a new array you could just keep appending new arrays in v

-A

#78693
Jun 9, 2006 at 4:05pm

thanks andrew, that seems to work nicely!

example patch:

#P window setfont Geneva 9.;
#P window linecount 1;
#P message 300 158 60 196617 dump parts;
#P comment 165 116 116 196617 add items to array parts;
#P message 165 171 84 196617 vary parts 2 leg;
#P message 165 152 95 196617 vary parts 1 torso;
#P message 165 133 89 196617 vary parts 0 arm;
#P comment 300 121 172 196617 print the first 3 items in the arrays;
#P comment 46 114 100 196617 add items to array 0;
#P message 46 167 77 196617 vary 0 2 three;
#P message 30 80 132 196617 makenew 0 , makenew parts;
#P message 300 140 42 196617 dump 0;
#P newex 165 220 18 196617 t l;
#P message 46 148 70 196617 vary 0 1 two;
#P message 46 129 68 196617 vary 0 0 one;
#P newex 121 267 58 196617 js test1.js;
#P comment 30 66 142 196617 first make a couple of arrays;
#P fasten 6 0 1 0 35 224 126 224;
#P connect 4 0 1 0;
#P connect 14 0 4 0;
#P connect 12 0 4 0;
#P connect 11 0 4 0;
#P connect 10 0 4 0;
#P connect 7 0 4 0;
#P connect 5 0 4 0;
#P connect 2 0 4 0;
#P connect 3 0 4 0;
#P window clipboard copycount 15;

—–test1.js——–

v = new Array();

function makenew(x)
{
v[x] = new Array();
}

function vary(x,n,b)
{
var jones = new Array();
jones = v[x];
jones[n] = b;

}

function dump(n)
{
var temp = new Array();
temp = v[n];
post(n,” :”,temp[0],temp[1],temp[2],”n”);
}

——-end test1.js———-

#78694
Jun 10, 2006 at 12:45am

Plan B – the javascript eval() function:

function addToArray()
{
a = arrayfromargs(arguments);
if(eval(“!” + a[0])) // test for non-existence
{
eval(a[0] + ” = [" + a.slice(1) + "]“);
}
else if(eval(“typeof(” + a[0] + “.slice) == “function”")) // test for array
{
eval(a[0] + ” = ” + a[0] + “.concat(” + a.slice(1) + “)”);
}
else
{
post(a[0] + “: not an arrayn”);
return;
}
post(“array ” + a[0] + “: “);
eval(“post(” + a[0] + “)”);
post(“n”);
}

function clearArray(aname)
{
if(eval(“typeof(” + a[0] + “.slice) == “function”")) // test for array
{
eval(aname + ” = []” );
post(“array ” + aname + “: clearedn”);
}
else
{
post(a[0] + “: not an arrayn”);
}
}

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P message 117 194 88 196617 clearArray test;
#P message 211 194 199 196617 addToArray test 2 3 1 5 6;
#P newex 211 268 50 196617 js;
#P connect 2 0 0 0;
#P connect 1 0 0 0;
#P window clipboard copycount 3;

#78695
Jun 10, 2006 at 12:57am

cool…the eval function is new to me!
but I guess what I meant is how can i initialize an array dynamically. These are for manipulation of an existing array, correct?

p.

#78696
Jun 10, 2006 at 1:53am

eval() can do anything you want dynamically. this is the big bonus of
an interpreted language. go nuts. i sure do. there are user-code
insertion points all over my app. ;)

#78697
Jun 10, 2006 at 2:39am

Using eval in this way will probably be noticeably slower, FWIW. I
would recommend exploiting object properties rather than John’s
example code, though sure, eval is useful in many contexts (see
dynexpr.js example) when you need true dynamism.

-Joshua

#78698
Jun 12, 2006 at 4:32am

Quote: pnyboer wrote on Sat, 10 June 2006 12:57
—————————————————-
> These are for manipulation of an existing array, correct?

No, the code will create the array for you if it doesn’t already exist, for both the add and clear functions.

Also does some checking (for presence of the the array.slice function) to ensure that you’re not overwriting existing variables that aren’t arrays – this is pretty much essential when using eval() to process input. You could just make a function that eval()s any symbol you poke at it and send it line after line of js from the outside world, but that could definitely be considered a little … unwise.

As Joshua notes, eval() is not fast – I’d be wary of using this for anything realtime, I tend to use eval() for dynamic patch/variable setup, processing js-based preference files, etc.

#78699

You must be logged in to reply to this topic.