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 › simple variable scope question
Page Index Toggle Pages: 1
simple variable scope question (Read 526 times)
simple variable scope question
Apr 7th, 2008, 8:25pm
 
New to processing, but familiar with "scripting".

I'm used to something like this working just fine:
Code:

// A
float firstVar = 1.0;
float secondVar = 2.0;
float thirdVar = 3.0;
float[] myArray; // changing this line...
//float[] myArray = new float[3]; // ...to this doesn't make a difference.
if(true){
myArray = {firstVar, secondVar, thirdVar};
}
//

But it does not. I get an "unexpected token" when defining 'myArray' inside the if.  The next two examples however, DO work, but 'B' seems really clunky, and why 'C' works and 'A' doesn't confuses me:
Code:

// B
float firstVar = 1.0;
float secondVar = 2.0;
float thirdVar = 3.0;
float[] myArray = new float[3];
if(true){
myArray[0] = firstVar;
myArray[1] = secondVar;
myArray[2] = thirdVar;
}
//

// C
float firstVar = 1.0;
float secondVar = 2.0;
float thirdVar = 3.0;
float[] myArray = {firstVar, secondVar, thirdVar};
//

So it seems like the scope of the if isn't allowing me to update myArray in one go...?  Is there a cleaner way to do this?  Many thanks.
Re: simple variable scope question
Reply #1 - Apr 7th, 2008, 8:30pm
 
I think the issue is that the {1,2,3} syntax *only* works when constructing an array, not later. To do it later you need to do:

Code:
float[] myArray;

myArray=new float[]{1,2,3};
Re: simple variable scope question
Reply #2 - Apr 7th, 2008, 10:34pm
 
Sho'nuff, that worked, many thanks.  But that brings up another question.. again, stemming from my noobiness of the language:
I would think that declaring 'myArray' as a 'new float[]' inside of a loop... that would 'separate' it from it's parental variable scope, and make it local to the loop.  But that's obviously not happening.  What's the reasoning behind that?  Again, thanks.
Re: simple variable scope question
Reply #3 - Apr 7th, 2008, 10:45pm
 
if you added "float[] " before the line in the loop, it would create a second, local array called myArray, but if you don't give it a type, it just changes the global version.

Code:

float[] myArray; //giving type = new variable.
for(something...)
{
myArray=new float[]{1,2,3}; // assigns value to array as a whole
}
for(somethingelse...)
{
float[] myArray=new float[]{1,2,3}; //this is a seperate array called myArray,
//this one is local, and you'll be unable to access the global one
//for the rest of this loop.
}

Re: simple variable scope question
Reply #4 - Apr 7th, 2008, 11:04pm
 
Ok, I'll buy that.  FYI, in your example, and in one's I've tried to recreate:  The duplicate local variable tosses an error to the effect:
Code:
"Duplicate declaration of local variable "myArray".  The other occurrence is at location...." 


I presume duplicate named local variables are allowed.  Was there just a simple mistake in the example?  This is starting to tangent from my orig post, but it really helps.
Re: simple variable scope question
Reply #5 - Apr 7th, 2008, 11:10pm
 
Ah yes, sorry.. the 2nd one would have to be within a function or something., so you can have global and local variables with the same name.
Re: simple variable scope question
Reply #6 - Apr 8th, 2008, 12:18am
 
Thanks for the help and clarity.
Page Index Toggle Pages: 1