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

Creating variables in loop

Status
Not open for further replies.

dragonturtle

Programmer
Sep 25, 2003
55
CA
Hi,

is it possible to create variable names in a loop? For example, in the following loop I'm trying to create variables named V0, V1, V2, etc. and assign them values 0, 1, 2, etc., but V+i doesn't work.

Code:
for (i=0; i<10; i++){
  var V+i = i;
}

I cannot use an array because these variables are going to be applied to a formula queried from a database (e.g. if the formula is V0*V1+V2, the page will show a result of 2 in this case).

Thanks.
 

Code:
for ( var i=0; i < 10; i++ ) {

	eval( &quot;var v&quot; + i + &quot; = &quot; + i );
}

A tip for the future: don't get too friendly with eval() if you can avoid it. ;o)
 

Oops - forgot to mention that you cold use an associative array if you wanted.

Code:
	var var_list = new Object();

	for ( var i=0; i < 10; i++ ) {

		var_list[ &quot;v&quot; + i ] = i;
	}

You would access the values like:
Code:
var_list[ &quot;v3&quot; ]

HTH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top