# Loop thru similarly named variables

**URL:** <https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371>\
**Category:** flash\
**Created:** [May 14, 2008, 9:31pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371 "2008-05-14T21:31:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [May 14, 2008, 9:31pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/1 "2008-05-14T21:31:30Z")

</div>

OK, I’ll probably get flamed a bit for this because I know I could find related threads if I do a search. But I have found answers to related questions confusing. So here goes…

I have a “reset” function that removes MCs called head01, head02, head03,… head20. The way I’ve done it is as follows, because I don’t know how to loop through it:

```php

if (head01) {
    if (head01.parent != null) {
     removeChild(head01);
    }
}
if (head02) {
    if (head02.parent != null) {
     removeChild(head02);
    }
}
if (head03) {
    if (head03.parent != null) {
     removeChild(head03);
    }
}
 
// etc. through head20.

```

How do I do this in a loop?

Thanks!  
Tim

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 14, 2008, 10:24pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/2 "2008-05-14T22:24:32Z")

</div>

```auto
for (var i:int=1;true;i++) {
    var childName:String="head"+(i.toString().length==1?"0"+i:i);
    var child:DisplayObject=this.[childName];//this.getChildByName(childName);
    if (!child) break;
    if (!child.parent) removeChild(child);
}

```

}

---

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [May 15, 2008, 6:25pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/3 "2008-05-15T18:25:57Z")

</div>

[quote=Felixz;2326357]ActionScript Code:  
[LEFT][COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000ff]int[/COLOR]=[COLOR=#000080]1[/COLOR];true;i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]\</p\>  
\<p\> [COLOR=#000000] **var** [/COLOR] childName:[COLOR=#0000ff]String[/COLOR]=[COLOR=#ff0000]“head”[/COLOR]+COLOR=#000000[/COLOR];\</p\>  
\<p\> [COLOR=#000000] **var** [/COLOR] child:DisplayObject=[COLOR=#0000ff]this[/COLOR].[COLOR=#000000][[/COLOR]childName[COLOR=#000000]][/COLOR];[COLOR=#808080]_[//this.getChildByName](https://this.getChildByName)(childName);\</p\>_[/COLOR]  
\<p\> [COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] [COLOR=#0000ff]break[/COLOR];\</p\>  
\<p\> [COLOR=#0000ff]if[/COLOR] COLOR=#000000[/COLOR] removeChildCOLOR=#000000[/COLOR];\</p\>  
\<p\>[COLOR=#000000]}[/COLOR][/LEFT]

[/quote]

Thanks Feliz! I had a feeling you might be the one responding to my post.

I incorporated this without really understanding all the elements. (If you’re so inclined, I would appreciate a line-by-line explanation.) I did get the following error referencing the line containing “[COLOR=#000000] **var** [/COLOR] child:DisplayObject=[COLOR=#0000ff]this[/COLOR].[COLOR=#000000][[/COLOR]childName[COLOR=#000000]][/COLOR];”:

_1084: Syntax error: expecting identifier before leftbracket._

Any help with that?

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 15, 2008, 8:13pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/4 "2008-05-15T20:13:13Z")

</div>

remove that dot  
and if this won’t work either try commented code

---

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [May 15, 2008, 9:12pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/5 "2008-05-15T21:12:18Z")

</div>

[quote=Felixz;2326816]remove that dot  
and if this won’t work either try commented code[/quote]

Neither works on my end.

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 15, 2008, 9:15pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/6 "2008-05-15T21:15:49Z")

</div>

Are head clips have set name property?

---

<div class="post-metadata">

**Author:** ![tpann](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tpann](https://forum.kirupa.com/u/tpann)\
**Post date:** [May 15, 2008, 9:47pm UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/7 "2008-05-15T21:47:47Z")

</div>

> [@Felixz;2326863](#):
>
> Are head clips have set name property?

No. They’re all created like this:

```php

private var head0x:Sprite;
addChild(head0x);
etc.

```

---

<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:** [May 16, 2008, 12:57am UTC](https://forum.kirupa.com/t/loop-thru-similarly-named-variables/260371/8 "2008-05-16T00:57:48Z")

</div>

hey tpann

you could either set the names of the sprites:

```auto
head0x.name = "head0x";

```

then Felixz’s should work

or

you could create an array with references directly to all the heads and delete them there:

```auto

private var headArr:Array = new Array();
private var head0x:Sprite;
addChild(head0x);
headArr.push(head0x);
etc.[COLOR=#000000][COLOR=#007700][/COLOR][/COLOR]

```

and then to delete

```auto

for(var i = 0; i < headArr.length; i++){
   this.removeChild(headArr*;
}

```

hth
