We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › dynamically referencing variables
Page Index Toggle Pages: 1
dynamically referencing variables (Read 381 times)
dynamically referencing variables
May 12th, 2008, 8:35pm
 
Hi,

is there an easy way to dynamically reference variables in processing?

Code:

int test1 = 0;
int test2 = 20;
int test3 = 20;

for( int i=1; i<=3, i++){

"test"+i = 100; // var test1 should now be 100

}


i think ["test"+i] is the way its done in actionscript, is there a way to do this in processing?

any help appreciated!
Re: dynamically referencing variables
Reply #1 - May 12th, 2008, 8:47pm
 
You want arrays:

Code:
int test[3];
test[0]=0;
test[1]=20;
test[2]=20;

for(int i=0;i<3;i++)
{
test[i]=100;
}
Re: dynamically referencing variables
Reply #2 - May 13th, 2008, 6:51pm
 
Funny, some languages used to do that...
In this simple case, arrays is indeed the right tool.
For more complex cases, you might want to look at HashMap or other associative arrays.
Page Index Toggle Pages: 1