# For loop help much appreciated please!

**URL:** <https://forum.kirupa.com/t/for-loop-help-much-appreciated-please/224862>\
**Category:** flash\
**Created:** [May 6, 2007, 1:58pm UTC](https://forum.kirupa.com/t/for-loop-help-much-appreciated-please/224862 "2007-05-06T13:58:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![PrinceTaj](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@PrinceTaj](https://forum.kirupa.com/u/PrinceTaj)\
**Post date:** [May 6, 2007, 1:58pm UTC](https://forum.kirupa.com/t/for-loop-help-much-appreciated-please/224862/1 "2007-05-06T13:58:37Z")

</div>

Hi Guys!

Okay, the scenario is as follows. In the below code exert, I have attached a movie clip (square\_mc) and made three duplicate instances of it. What I would like to happen is that when the user clicks on the delete\_btn button component, it removes all of the duplicate instances. What I have done is extracted all the movie clips on stage and put them in an array called mc. Then in the event callback handler, a for loop iterates through all the movie clips in the mc array, and checks the .\_name property of each clip. If .\_name == “square\_mc”+j, then it deletes that instance.

However, the problem is, the compiler doesn’t seem to like the concatenation of j, which corresponds to the loop control variable in this expression. When I replace j with a number that corresponds to the instance, i.e. .\_name = “square\_mc”+0, then it works. Obviously, I don’t want to have to hard code instance names, I want it to be dynamic. Is there any reason why the compiler doesn’t like it when I concatenate j with the string???

Below is my code exert:

```auto

this.attachMovie("square", "square_mc", 10);

square_mc.duplicateMovieClip("square_mc"+0, 11,
                                {_x:50});
square_mc.duplicateMovieClip("square_mc"+1, 12,
                                {_x:100});
square_mc.duplicateMovieClip("square_mc"+2, 13,
                                {_x:150});

var mc:Array = new Array(); // Array holds the all the movieclips on stage
var index:Number = 0; // Used for indexing mc Array

// Loop through all movie clips on stage
for (var i in this) {
    if (typeof (this*) == "movieclip") {
        mc[index] = this*;
    }
    index++;
}

var listener:Object = new Object; // Declare listener object for component event

// Event callback handler function
// Iterates through mc Array to extract square_mc instances
// then removes/unloads them from stage
listener.click = function(evtObject:Object) {
    for (var j=0; j<mc.length; j++) {
        if (mc[j]._name == "square_mc"+j) {
            mc[j].unloadMovie();
        }
    }
}

delete_btn.addEventListener("click", listener); // Register listener with component

```

Any help would be greatly appreciated. I am completely puzzled as to why it just read the value for j.

Many thanks guys

TAJ
