# For loop only run once?

**URL:** <https://forum.kirupa.com/t/for-loop-only-run-once/262801>\
**Category:** flash\
**Created:** [June 9, 2008, 11:24pm UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801 "2008-06-09T23:24:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikomax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nikomax/32/3664_2.png) [@nikomax](https://forum.kirupa.com/u/nikomax)\
**Post date:** [June 9, 2008, 11:24pm UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/1 "2008-06-09T23:24:48Z")

</div>

Hey there Guys/Gals,

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

```auto

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

> **[Google](https://www.google.com)**
>
> Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.

This is simply a text part

> **[Home](https://www.ask.com)**
>
> Answers you want. Content for days. What more could you Ask for?

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

```html

<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

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [June 10, 2008, 4:19am UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/2 "2008-06-10T04:19:30Z")

</div>

Did you try the following

```auto

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

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [June 10, 2008, 4:42am UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/3 "2008-06-10T04:42:18Z")

</div>

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

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

```

to

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

```

don’t know if that’s it.

---

<div class="post-metadata">

**Author:** ![Ikinsey](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Ikinsey](https://forum.kirupa.com/u/Ikinsey)\
**Post date:** [June 10, 2008, 8:28am UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/4 "2008-06-10T08:28:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nikomax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/nikomax/32/3664_2.png) [@nikomax](https://forum.kirupa.com/u/nikomax)\
**Post date:** [June 10, 2008, 11:28am UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/5 "2008-06-10T11:28:56Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Ikinsey](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Ikinsey](https://forum.kirupa.com/u/Ikinsey)\
**Post date:** [June 10, 2008, 10:44pm UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/6 "2008-06-10T22:44:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ikinsey](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Ikinsey](https://forum.kirupa.com/u/Ikinsey)\
**Post date:** [June 10, 2008, 10:47pm UTC](https://forum.kirupa.com/t/for-loop-only-run-once/262801/7 "2008-06-10T22:47:10Z")

</div>

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;  
}
