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 › how do i make loadString use the array i declered
Page Index Toggle Pages: 1
how do i make loadString use the array i declered (Read 953 times)
how do i make loadString use the array i declered
Mar 6th, 2010, 6:31am
 
hi
i seem to have to declare the lines[] array when using loadString,
but if cant call loadString from outside setup. how can use it globaly?
Code:

String lines[] ;
void setup(){
 String lines[] = loadStrings("everyJPG.log");
}
void draw(){
 println("there are " + lines.length + " lines");
}



scope, var, array scope, loadString
Re: how do i make loadString use the array i declered
Reply #1 - Mar 6th, 2010, 6:39am
 
obviously wrong? but works

Code:

//String lines[]=new String[99999];
String names[]=new String[99999];

void setup(){

 String lines[] = loadStrings("everyJPG.log");
 arraycopy(lines, names);
 println("there are " + lines.length + " lines");

 noLoop();
}
void draw(){
println("there are " + names.length + " lines");

 for (int i=0; i < names.length; i++) {
   println(names[i]);
 }

}

Re: how do i make loadString use the array i declered
Reply #2 - Mar 6th, 2010, 8:39am
 
When you write String lines[], you are creating a new array of Strings named lines.  And there can be multiple variables names lines -- in your case one exists globally and one only exists inside setup(), which then "obstructs" the global variable.

Try lines = loadStrings(...) inside setup() of your first post.  This way, you are assigning the result of loadStrings(...) to the global variable, rather than to a newly created local one inside setup().
Re: how do i make loadString use the array i declered
Reply #3 - Mar 7th, 2010, 2:41am
 
To clarify a little further: To declare your array globally you don't have to set its length, which is just as well when you don't already know it; and it avoids the need for this kind of horrible kludge:

Code:
String names[]=new String[99999]; 



This is really, really inefficient and should be avoided altogether...  Instead try:


Code:
// declare array globally
String[] lines;

void setup(){
 // Now it's just a matter of populating it:
 lines = loadStrings("everyJPG.log");
}

void draw(){
 println("there are " + lines.length + " lines");
}


If you want some clarification on variable scope this might be helpful.
Page Index Toggle Pages: 1