For loop only run once?

Hey there Guys/Gals,

I’m trying to get all the data out of a chunk of xml so i wrote this code:


for (var i:uint; i < infoGroup.length(); i++) {
     trace(infoGroup.@sectionName*);
          for (var j:uint; j<infoGroup*.infoPoint.length(); j++) {
               trace(infoGroup*.infoPoint[j].infoText.text());
               trace(infoGroup*.infoPoint[j].infoRef.text());
          }
     trace("-end of section-");
} 

How ever the second for loop is only run once, Why is that? Here is the output and the xml.

background
This is some information regarding the background of whatever I am talking about in the XML file

This is simply a text part

-----end of section-----
main Information
-----end of section-----
Misc Information
-----end of section-----


<infoGroup sectionName="background">
        <infoPoint>
            <infoText>This is some information regarding the background of whatever I am talking about in the XML file</infoText>
            <infoRef>http://www.google.com</infoRef>
        </infoPoint>
        <infoPoint>
            <infoText>This is simply a text part</infoText>
            <infoRef>http://www.ask.com</infoRef>
        </infoPoint>
    </infoGroup>

<!-- the other section have the same format! -->


Its easy to see from the out put that the second for loop is only run once =[

Thanks for you help! <3

Did you try the following


for (var i:uint; i < infoGroup.length(); i++) {
     trace(infoGroup.@sectionName*);
          for (var j:uint = 0; j<infoGroup*.infoPoint.length(); j++) {
               trace(infoGroup*.infoPoint[j].infoText.text());
               trace(infoGroup*.infoPoint[j].infoRef.text());
          }
     trace("-end of section-");
}

hth

The way i understood it was:

His second for loop runs once (output google and and ask)
then his first loop runs again, after that his second for loop doesn’t run anymore.

I think his j is not reset so it will stay j = 2, that’s why I edited the line

for (var j:uint; j<infoGroup*.infoPoint.length(); j++) {

to

for (var j:uint = 0; j<infoGroup*.infoPoint.length(); j++) {

don’t know if that’s it.

For future loops which access XML, I recommend using a for each loop instead.

@hasch2o,

That was exactly it =D.
I guess I thought that the var j would be reset when the 1st loop was called again. Thanks a-lot

@Ikinsey

Why for each? Is it better? will look into it though =D

Thanks everyone

It takes a lot of the work out of it for you. If you use E4X with it, you can really quickly and cleanly parse through XML.

Let’s say you had some XML:

<food>
<sandwich breadType=“rye”>
<ham ounces=".4"/>
</sandwich>
<sandwich breadType=“white”>
<ham ounces=".3"/>
</sandwich>
<apple/>
<brocolli/>
<ham ounces=".2"/>
</food>

You could figure out how many ounces of ham were in

Whoops!
Anyway

you could figure out how many ounces of ham you had by using:

var totalHam:Number = 0;
for each(var ham:XMLNode in food…ham)
{
totalHam += ham.@ounces;
}

or even more to the point

for each(var hamOunces:Number in food…ham.@ounces)
{
totalHam += hamOunces;
}