# \[F8\] URGENT! Looping through array

**URL:** <https://forum.kirupa.com/t/f8-urgent-looping-through-array/266440>\
**Category:** Uncategorized\
**Created:** [July 20, 2008, 5:40pm UTC](https://forum.kirupa.com/t/f8-urgent-looping-through-array/266440 "2008-07-20T17:40:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![avukovi4](https://avatars.discourse-cdn.com/v4/letter/a/e8c25b/32.png) [@avukovi4](https://forum.kirupa.com/u/avukovi4)\
**Post date:** [July 20, 2008, 5:40pm UTC](https://forum.kirupa.com/t/f8-urgent-looping-through-array/266440/1 "2008-07-20T17:40:35Z")

</div>

**SHORT EXPLANATION:**  
I have 5 cards on the stage (named plocica0 to plocica4) and dynamic text (named pojam) in each.  
In code, I have 3 different arrays (named niz1, niz2, niz3) populated with sentences. I’ve created a function which have to populate these cards with sentences from given arrays but without repeating them - meaning each card should have different sentence inside.

**I need next procedure (if possible):**

- choose five sentences (one by one) from given arrays (without changing them)
- push them into new array
- then, **if possible** , **while populating new array with sentences** , compare them - in my function I’ve tried to create a procedure of comparsion in a way of pushing the last chosen sentence into array and then, with a counter, loop through previous sentences in that array and **if found two same** sentences, delete last one and **repeate** the procedure of choosing as long as they are the same
- the whole procedure of choosing sentences should last as long as all five of them aren’t different

**My code:**

```php
var niz1 = Array("Volim te.","Ljubavi moja.","Ti si ze&#269;i&#263;.","Danas je lijepi dan.");
var niz2 = Array("Ju&#269;er je bilo lijepo.","Sutra je ponedjeljak.","Ti znaš plivati?","Danas radim.");
var niz3 = Array("Dusche dich am Vormittag nicht!","Zeko piše.","Mario fotografira.","Ma&#269;ke spavaju.");

function chooseSentences() {
    var chosenSentences = new Array();//container for chosen sentences
    var chosenIndex = new Array();
    var chosenArrElement:Object = new Object();
        
    for (var i = 0; i < 5; i++) {
        var chooseArr = random(3) + 1;
        var chooseElement = random(4);
        chosenSentences* = this["niz" + chooseArr][chooseElement];
        trace("chosenSentences original: "+chosenSentences*);
        
            var j = 0;
            while (j < i) {
                if (chosenSentences* == chosenSentences[j]) {
                    do {
                    var popped:Object = chosenSentences.pop();
                    trace("popped: "+popped);
                    chooseArr = random(3) + 1;
                    chooseElement = random(4);
                    chosenSentences* = this["niz" + chooseArr][chooseElement];
                    trace("chosenSentences new choice: "+chosenSentences*);
                    }
                    while (chosenSentences* == chosenSentences[j]); 
                }
                else {
                    j++;
                }
            } //end while (j<i)
        

        chosenArrElement.array = chooseArr;
        chosenArrElement.element = chooseElement;
        chosenIndex* = [chosenArrElement.array,chosenArrElement.element];
    }
    trace("final: "+chosenSentences);
    trace("chosenIndex: "+chosenIndex);    

    //writting into cards:
    for (var k = 0; k < chosenSentences.length; k++) {
        _root["plocica" + k].pojam = chosenSentences[k];
    } 

} // End of function
stop();

onLoad = function() {
    chooseSentences();
}  

```
