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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Naming array Variables -- need help ! 1

Status
Not open for further replies.

justjim

Programmer
Oct 22, 2002
13
US
I need to be able to establish dynamically a changing
number to arrays, who naming convention would be like:

rNums changes each time the code is run

for(i=0;i<rNums+1;i++){
var aRay = new Array(variableSizes)
}

does anyone know if it is possible/ or Not to dynamically set variable names on the Fly ?

Thanks Jim
 
i'm not sure i follow the what or why of what you're trying to do, but you can create varaible names (or any code for that matter) on the fly using the eval function. for example,

Code:
var i = 1;
eval(&quot;var s&quot; + i + &quot; = 'hello'&quot;);

from the above point on, you have a variable called &quot;s1&quot;.

hope that helps,

glenn
 
actually there is an nice thing about Arrays in JavaScript. You do no need to worry about size before creating it nor when you want to add to it.

Here is an example of the push() method and how it adds an element at the end of the Array.

var myArray = new Array();
myArray.push(&quot;this is a string&quot;)
myArray.push(&quot;and another&quot;)
myArray.push(&quot;and again&quot;)

Arrays in JS are a lot like vectors in Java. You can add anything to them like objects and don't have to worry about some being one type and others being another type.

var myObject = new Object();
myArray.push(myObject)
myArray.push(&quot;see I can still add a string after&quot;)

Then you can use the join(character) method where you give it a string that will seperate the sets of information inside your array.

alert(myArray.join(&quot;\n&quot;))

I hope this helps! :) Gary Haran
 
A Star for Gary, for his &quot;short and clear&quot; post. Water is not bad as long as it stays out human body ;-)
 
Thanks Guys,

I had tried the Eval method to setting a variable but must have screwed it up
some how.... but that is nothing new :))

Thanks also, for the array tips... I am sure those will come in handy as I
start of ill and manipulate the data in these arrays ...

Next Question is ... ? Once the variables have been created LIke :

s1, s2, s3, ........... ,s(n).etc.

How do i refer to the varibale programatically ?
would i use something like :

for ( i=numOfVars; i<numOfVars+1; i++) {
alert ( &quot; On The Fly Variable = &quot; + s )
}

Thanks Again Guys ... Jim
 
Hope this helps(try runnig it to see what it does):

Code:
<script>
for (i=0;i<10;i++){
	eval (&quot;arr&quot; + i + &quot;= new Array(&quot; + i*i*i + &quot;)&quot;)
}    

for (i=0;i<10;i++){
	document.write (eval (&quot;arr&quot; + i + &quot;.length&quot;)+&quot;<br>&quot;)
}

</script>
 
I Again tried the EVAL method and still get errors

here is how i wrote the code : 'rline' is a previously established varible with
contents ... i want to place its contents into the on-the fly variable :
TreeLine[r] ....

eval( &quot;var TreeLine &quot; + r + &quot; = &quot; + rline);
alert ( r + &quot; 's RLINE = &quot; + rline );

What am i doing Wrong ?

Thanks, Jim
 
I mis-typed the code above as well as being incorrect syntax ...

eval( &quot;var TreeLine &quot; + r + &quot; = &quot; + rline);
One major flaw was the (space) here ^

WHEW !!!
I finally got the code syntax correvt and working with : .....

eval(&quot;var TreeLine&quot; + r + &quot; = '&quot; + rline + &quot;'&quot; );
alert ( &quot;TreeLine&quot; +r + &quot; = &quot; + eval ( &quot;TreeLine&quot; + r ) )

Thanks everyone for the help .... Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top