Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Naming an Array using a variable or expression

Status
Not open for further replies.

finooch

Technical User
Jul 22, 2002
2
US
Okay Folks -

Long time lurker, first time poster.

I have a movie that loads a number of delimited strings from text files.

I use loadVariables to bring in, say, strings.txt. This file contains a number of delimited variables thusly:
strng1=foo1$foo2$foo3$foo$&
strng2=fie1$fie2$fie3$fie4&
etc...

The goal is to create and populate a multidimensional array using a for loop. Here is a snippet of code that doesn't work, but represents what I'm trying to accomplish:

for (i=0; i<myotherarray.length; i++) {
j=0;
str = &quot;strng&quot;+(i+1);
strn= str.split(&quot;$&quot;);
set(anarrayname, new Array(new Array(strn[j],strn[j+1],strn[j+2],strn[j+3],strn[j+4])));
}

So, when tested as written above, anarrayname[0][0] will return the literal value of str. Also, anarrayname[0] will return an array with the literal value of str as the first element and the rest undefined.

If I replace

strn= str.split(&quot;$&quot;);
with

strn= [str].split(&quot;$&quot;);

none of the array locations are populated.

Obviously, what I want if for str to be recognized as the name of a string, so that the .split will be performed on the correct string and populate the array. Just can't seem to make it happen.

HELP!!??!!
 
Have you tried creating the multi-dimensional arrays as Objects pushed on to an array stack?

Something like this:

myArray=new Array;
myArray.push({name:'me', value:2});

I've used this syntax with good results. It's easy to populate the Object's content from a loop and, depending on how you set things up, you can access its properties from the 'parent' array with standard myArray[0][0] syntax or as an associative array accessing object values by name.
 
Wangbar -

Thanks for such a prompt response! Two questions (one's prolly a dumb one):
First, in this line:
myArray.push({name:'me', value:2});

You're using a { curly-bracket (or a Bob Hope, if you prefer). I'm not really clear on how that bracket is interpreted compared to the [ straight-bracket, which I understand from other documentation to be the bracket one would use around an expression. Is there a specfic syntax difference?

Second, can I Push an array into an array?

Thanks Again,
finooch
 
The curly bracket is used when initialising a custom object:

myObject = { name1 : value1 , name2 : value2 ,... nameN : valueN };

If you create an object in this way and push it on to an array it effectively gives you a multi-dimensional array. the square brackets are used for addressing movieclips and such as if they are array values - very handy.

_parent['clip']._x=100;

I've never tried pushing an array on to an array, worth a try...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top