PDA

View Full Version : arrays inside a loop


shockthealien
11-03-2003, 12:09 PM
Hi, im trying to call the elements of a set of arrays using a loop, but just banging my head against a wall here.

Basically i have loaded a set of products, each one in an array and im trying to list them. I have a set of textboxes ready to display the data, and i created a simple loop to put the data in the textboxes, but no luck so far... here's the loop:

for (i=1; i <= eval("_level0.section"+sec+"_nit"); i++){
_root["dishName"+i] = _level0["s"+sec+"_item"+i][0]);
_root["dishDesc"+i]= _level0["s"+sec+"_item"+i][1]);
_root["dishPrice"+i]= _level0["s"+sec+"_item"+i][(_level0.locNum+1)]);
}

Any ideas? thanx in advance

black
11-03-2003, 12:18 PM
first, trace this line to see the if the result what you expected:
eval("_level0.section"+sec+"_nit");

and if still problems, search the path of your array and make sure of you implemented them before using, such as this below:
arr = new Array();

shockthealien
11-03-2003, 12:30 PM
hi, thanks for such a quick reply.

I have done the trace u said and yes, it returns as expected the number of items in that section.

And yes, the arrays are defined in a previous step and full of data waiting to be pulled

The error i get with this code when i publish it is Syntax error, so i dont know what im doing wrong here as i cant find much documentation in this square bracket syntax covering arrays

Eric Echols
11-03-2003, 04:00 PM
It appears that you probably have a naming issue.

for (i=1; i <= eval("_level0.section"+sec+"_nit"); i++){
_root[ "dishName"+i ] = _level0["s"+sec+"_item"+i][0]);
_root[ "dishDesc"+i ]= _level0["s"+sec+"_item"+i][1]);
_root["dishPrice"+i]= _level0["s"+sec+"_item"+i][(_level0.locNum+1)]);
}
{

Unless you have named your textboxes dishName1, dishName2, etc.... you aren't refering to any data.

Is the first Array instatiated as a multidimensional array? If so, then you should separate the textbox SymbolID and the incrementor variable(no + either). ie...

_root["dishName"][i] = _level0["s"+sec+"_item"][(_level0.locNum+i)];

Hope this helps..

shockthealien
11-03-2003, 04:15 PM
yes, i have textfields named dishName1, dishDesc1 and dishPrice1.... and so on.

I know that bit works, because if i change it to:

for (i=1; i <= eval("_level0.section"+sec+"_nit"); i++){
_root[ "dishName"+i ] = "Any String Value";
}

I get all the items called "Any String Value". So i guess the problem is naming the array at the other end using this square bracket syntax

_level0["s"+sec+"_item"+i][0])

According to the only reference i've found, it looks as it should be ok, but doesnt work...

black
11-04-2003, 10:26 AM
if your problem hidden whin this line: _level0["s"+sec+"_item"+i][0]) then plz check if you declare the 2d array accurately, ie the second dimention must be declared before we handle with them.
like this:


// The first dimention~
arr = new Array;
// The second dimention~
arr[0] = new Array();
// If the second dimention havnt been implementd before this line will cause an error !
arr[0][0] = 1;
trace(arr);


hope this helps~

shockthealien
11-04-2003, 10:31 AM
Problem solved... so embarrassed to admit that after all it just was a parenthesis too many at the end of the array declaration...

_level0["s"+sec+"_item"+i][0])

and should be

_level0["s"+sec+"_item"+i][0]

Sorry for wasting your time

black
11-04-2003, 10:59 AM
a great trap aha :D anyway Cheer man~