PDA

View Full Version : Problems building an array through a for-loop


ndee
08-11-2005, 01:17 AM
Call me a newbie, call me an amateur, and you'll be propably right, but that still won't solve this question:

I'm building this piece of script which reads out an XML file. All the nodes are run through with a for loop. The idea is that everytime the index of the for loop finds a new node, the contents of the node are placed into an array. This gives me the following code. Note that this is just a part of the total script.. obviously..

content = new Array();
for (var n = 0; n<xmlDoc_xml.firstChild.childNodes.length; n++) {
inhoud = xmlDoc_xml.firstChild.childNodes[n].firstChild.firstChild.nodeValue;
content[n] = inhoud;
trace(inhoud);
}

trace(content);
trace(content[1]);

The diagnostics:
* 'inhoud' works fine, it contains the same content as the node.
* Either content, as content[1] seems to be empty. trace(content); results in void, and trace(content[1]); results in undefined.

So obviously i'm doing something wrong here with defining the array, but it's 4.15 AM local time.. i'm tired and i ran over this script way to often to recorgnize any errors anymore.. I've run through the tutorials on actionscript.org and others, run F1 and kicked Google to look for me, but no results there either. So please, is there anybody who can figure out why this isn't working?

Many thanks in advance,

nd

oka_
08-11-2005, 04:23 AM
After having a quick look, I'd suggest trying

content.push(inhoud);

instead of
content[n] = inhoud;

might solve your problem, I can't remember if flash will let you do it your way or not.

-oka

ndee
08-11-2005, 08:50 AM
nope, no use..

thanks for the effort though :)

mambenanje
08-11-2005, 09:12 AM
I suggest you create a temporal array in the loop use it to collect the values, then when it is done, then equate it to content. give it a shot, I use it mostly when working with classes

ndee
08-11-2005, 10:54 AM
I suggest you create a temporal array in the loop use it to collect the values, then when it is done, then equate it to content. give it a shot, I use it mostly when working with classes

What would that look like?


for (var n = 0; n<xmlDoc_xml.firstChild.childNodes.length; n++) {
tempArray = new Array();
inhoud = xmlDoc_xml.firstChild.childNodes[n].firstChild.firstChild.nodeValue;
tempArray[n] = inhoud;
}
content = tempArray;

This wouldn't and doesn't work, because tempArray will be resetted for every new n.. Could you edit my script, that will be more clear to me..

thanks

mambenanje
08-11-2005, 11:04 AM
function createArray(xmlNode){
var temArray:Array
for(i=0;i<xmlNode.length;i++){
temArray[i]=xmlNode.firstChild.nodeValue;
}
return temArray;
}
var content:Array=createArray(xmlDoc_xml.childNodes.ch ildNodes);

not tested it anyway just wanted to show what I meant, when I use this approach in my classes it happens to solve the problem, and when I try to direclty add the values to an array instance that is a property of my class it does not go, in both php and flash

ndee
08-11-2005, 11:34 AM
function createArray(xmlNode){
var temArray:Array
for(i=0;i<xmlNode.length;i++){
temArray[i]=xmlNode.firstChild.nodeValue;
}
return temArray;
}
var content:Array=createArray(xmlDoc_xml.childNodes.ch ildNodes);

not tested it anyway just wanted to show what I meant, when I use this approach in my classes it happens to solve the problem, and when I try to direclty add the values to an array instance that is a property of my class it does not go, in both php and flash

hmm, i tried, but it didn't seem to work. I don't see so much difference either: why would this work and the script i had won't. It is still defining an array before the for loop, and filling the array with it.. Thanks though!

ndee
08-11-2005, 01:29 PM
<homer>Doh.</homer>

The thing was that this piece of script was placed into a function. And variables in functions should be.. returned. Right, i've done that and it works like a charm!

Thanks for the replies!