PDA

View Full Version : Array returns undefined outside for each


SimonH
02-21-2008, 12:20 PM
I'm working in Flash CS3 and i want to read data from XML which works fine. I want to add this data to an array (N-rows with 4 columns). Problems occur when i want to access the saved data in another "for" stage.

var Content:Array = new Array(); // this is the container for the rows,if i'm not mistaken
var answerA:String = "";
var answerB:String = "";

function handlerTest(event:Event):void {
var loader:URLLoader=URLLoader(event.target);
var myXML:XML=new XML(loader.data);
myXML.ignoreWhite=false;

for each (var child:XML in myXML.*) {
var i:int = 0;
Content[i] = new Array (child, child.@answer, answerA, answerB);
trace(Content[i]); // this trace outputs everything as it should
i++;
}

// next i want to add contents into textfields
for (var j:int = 0; j < 10; j++) {
var rb:TextField = new TextField();
rb.text = Content[j][0]; // this produces error since there is no data
trace(Content[j]); // this returns undefined for each loop
trace(Content[j][0]); // this also doesn't return the values that i stored into the array above
addChild(rb);
}
}

am i doing something wrong?

Wino
02-21-2008, 01:00 PM
You never defined Vsebina, that I can see. You defined an array named contents.

Try (or something similar):


rb.text = Content[j];
addChild(rb);

SimonH
02-21-2008, 01:20 PM
oh that is my bad... in my post i forgot to translate Vsebina to english Content! in my .fla the array's name was Vsebina, but this does not affect my problem.

Wino
02-21-2008, 01:26 PM
OK.. Now I see your problem.

You defined Content[array] inside the handlerTest function when you made your new array:


Content[i] = new Array (child, child.@answer, answerA, answerB);


When you exit the handlerTest function, Content[array] no longer exists. This is a scope problem. Define the Array outside the function, as you did with the Contents array.

xwielder
02-21-2008, 01:29 PM
At the top, you have "Contents";

In your code you refer to "Content" (no 's' at the end).

Wino
02-21-2008, 01:34 PM
xwielder,

It is more than that. His "new" statement declares the new variable each time inside the for loop. He'll need to assign his array values without the var ... = new ... statement.

My explanation the first time above wasn't clear in that. Sorry.

SimonH
02-21-2008, 01:38 PM
i did define it outside the handlerTest function. i don't understand why, within the same function, values from Content are available in "for each" but not in next "for".

maybe i'm not adding rows correctly?

i thought i can do it this way (an example):
var Content:Array = new Array();
Content[0] = new Array ("one", "two", "three", "four");
Content[1] = new Array ("five", "six", "seven", "eight");
Content[2] = new Array ("and", "so", "on", "on");

thus making a 3x4 array,
to which the trace(Content) would output:
one, two, three, four, five, six, seven, eight, and, so, on, on.

SimonH
02-22-2008, 11:10 AM
finally i found a way how to correctly form 2D arrays!!!
here's my updated code:


var Contents:Array = new Array(); // main array
var loader:URLLoader=URLLoader(event.target);
var myXML:XML=new XML(loader.data);
myXML.ignoreWhite=false;

for each (var child:XML in myXML.*) {
var i:int = 0;
var row = new Array(child, child.@answer, answerA, answerB); // THIS IS THE WAY
Contents.push(row); // TO MAKE A NESTED ARRAY!
i++;
}