5

EDIT: EVERY ANSWER BELOW ARE WORKING, THANKS FOR HELPING ME!

I'm currently learn about splicing an array in as3. So here's my code:

//import classes
import flash.utils.Timer;
import flash.events.*;

//variables
var Arr:Array=new Array();
var num:Number=0;
//set a timer and set timer limit of 10 times
var timer:Timer=new Timer(1000,10);

//add a listener to our timer object
timer.addEventListener(TimerEvent.TIMER, tick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,tock);

//tick function
function tick(e:TimerEvent):void{
    //i add an element each time the timer 'ticks'
    Arr.push(['index'+num]);
    num++;
}
//tock function
function tock(e:TimerEvent):void{
trace('array elements :'+Arr);//traces Arr elemnts
for(var i:int=0;i<Arr.length;i++){
    Arr.splice(i,1);// i've tried Arr.splice(0,1), but neither working
    trace('elemnts left : '+Arr);
}

I dont really understand the problem, but here's the result:

1.not every elements in Arr array have been removed 2.the maximum Arr's length is ten before spliced.BUT in the loop, its only splicing less than ten times, which it causes the problem above

anybody have an idea for this? Please help me out

Yudhis
  • 53
  • 7
  • 2
    Your for loop is looking at `Arr.length` which changes every time the loop iterates. The first time it iterates it checks if `i` (currently 0) is smaller than 10. Then you remove an item and increment `i` to 1. On the next iteration it checks if `i` (now 1) is smaller than 9. See what I'm getting at? –  Jul 25 '18 at 12:08
  • Not pretty sure i get it. But i get the point – Yudhis Jul 25 '18 at 14:18
  • Thanks for explaining bro! This gives me more to see whats happening – Yudhis Jul 25 '18 at 14:19

3 Answers3

6

There are simpler and faster options:

You can just set the Array's length to 0. That will effectively remove all its elements at once.

Arr.length = 0;

You can create a new empty instance of the Array class. That will not destroy the original object immediately, but if there are no references to it, it will be consumed by the Garbage Collector eventually, so you won't need to think of it.

// You can omit () with the "new" operator if there are no mandatory arguments.
Arr = new Array;
Organis
  • 7,243
  • 2
  • 12
  • 14
  • Thank you very much, I'll try ! – Yudhis Jul 25 '18 at 14:32
  • Give me time 'till tomorrow. 'Cause i have to log out now – Yudhis Jul 25 '18 at 14:32
  • Array literals const `array:Array = []` are considered to be idiomatic and also faster. – Florian Salihovic Jul 31 '18 at 10:03
  • 2
    @FlorianSalihovic No and no. The **[...]** record is plainly shorter. Then, I just tested both with 1 million then 10 millions loops and, frankly, there's no much difference in elapsed time, the competition ends with +-50ms both ways. It is not impossible that there actually **WAS** difference at some point, but not with recent SDK/player versions. – Organis Jul 31 '18 at 10:50
  • @Organis - Thanks for pointing that out. I would always refer to the https://sourceforge.net/adobe/flexsdk/wiki/Coding%20Conventions/#array-literals – Florian Salihovic Jul 31 '18 at 21:52
  • @FlorianSalihovic It is most certainly good to have a scripting style, just don't venture into thinking that your style is the best. I looked through your link, some cases are fine with me, some others are... meh. The bad thing, they imperatively command *do this, don't do that*. I vouch for readable and easy-to-understand script, and also for straight architecture, but sometimes there are no time and sometimes an ugly solution is smaller and easier than refactoring a handful of classes, thus I compromise how to do things. Then I **ALWAYS** check myself what actually is faster. – Organis Aug 01 '18 at 00:59
4
for(var i:int=0;i<Arr.length;i++)

This is why it doesn't splice everything. Every time this loop runs, Arr.length is decreased by 1 since you spliced it, so once it gets to i==5(sixth loop), the conditions become fulfilled as 'i'(5) is no longer less than Arr.length(5 left in array), thus the loop stops.

You conditions should be to splice as long as the array has more than 0 items. Try this instead:

for(var i:int=0;Arr.length>0;i++)

Also, splice works like this. Arr.splice(INDEX, AMOUNT TO REMOVE). In this case you can splice at index0 to remove them one by one from the bottom. thus the right way to write this is:

Arr.splice(0,1)

If your goal is just to empty the array, simply do

Arr.length = 0;

On a side note, you dont need to put those square brackets when pushing a new array,

Arr.push('index '+num);

works just as well.

user1234567
  • 333
  • 2
  • 10
  • Thank you very much! Those method are working the way i want. And now i know whats the real problem. Thank you everybody ! – Yudhis Jul 26 '18 at 08:41
  • Thank you very much! Those method are working the way i want. And now i know whats the real problem. Thank you everybody ! – Yudhis Jul 26 '18 at 08:41
1

you can write in this way. It will remove the entire elements

 function tock(e:TimerEvent):void{ 

     var i = Arr.length
    while (i--) {
        ...
        if (...) { 
            Arr.splice(i, 1);
        } 
    }    
}

otherwise you just reinitialize that array(Arr)

Sujatha Girijala
  • 1,141
  • 8
  • 20
  • I'll try it.Thank you friend! – Yudhis Jul 25 '18 at 14:31
  • Give me time 'till tomorrow. 'Cause i have to log out now – Yudhis Jul 25 '18 at 14:33
  • 1
    No, delete wont work like that. With it you can only delete properties from array, not empty it. – kaarto Jul 26 '18 at 15:33
  • Whats the different between empty an array and only removing the properties of it? What is not removed when we remove the properties only? – Yudhis Jul 27 '18 at 05:48
  • Whats the different between empty an array and only removing the properties of it? What is not removed when we remove the properties only? – Yudhis Jul 27 '18 at 06:05
  • 1
    If your array.length is 5, after `delete` it's still 5. It might empty some values, but it wont empty the array. And his example will cause only errors, as it can not be used directly to the array itself. – kaarto Jul 27 '18 at 08:28
  • 1
    So you can for example `delete Arr[0];` to empty the first property/value/element in it, but then the arrays length remains the same and Arr[0] will be undefined value. – kaarto Jul 27 '18 at 08:36
  • Ooh, now I got it.Thanks a lot @kaarto – Yudhis Jul 27 '18 at 11:49
  • @Kaarto I did not reply for loop. It is for remove entire elements without using for loop – Sujatha Girijala Jul 27 '18 at 12:21
  • @SujathaGirijala no, it doesn't work like that. You can not delete fixed properties or variables with `delete`. Try it yourself or read [here](https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#delete). – kaarto Jul 27 '18 at 12:25
  • @kaarto thanks for your reply. Now I edited that answer please have a look on it. when you are free – Sujatha Girijala Jul 27 '18 at 12:38