Or if you aren't deleting everything, put i-- next to each pop().

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Jobe
Makar
Sent: Monday, 21 November 2005 10:46 AM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Array Madness - test yourself

Hi Judah,

This is a good candidate for a 'while' loop:

while(errors.length >0) {
    errors.pop();
}

Jobe Makar
http://www.electrotank.com
http://www.electro-server.com
phone: 919-609-0408
mobile: 919-610-5754
fax: 919-341-8104
----- Original Message ----- 
From: "Judah Frangipane" <[EMAIL PROTECTED]>
To: "Flashcoders mailing list" <flashcoders@chattyfig.figleaf.com>
Sent: Sunday, November 20, 2005 6:48 PM
Subject: [Flashcoders] Array Madness - test yourself


>I just want to bring to your attention a rare array problem that eluded

> me for years. I just realized what is going on.
> 
> Whenever I tried to clear an array I would use a for loop, iterate 
> through all the items and pop each one off. Every once in a while the 
> arrays would still contain values and not be completely erased. Can
you 
> spot the error? Here is the code:
> 
> *****************************************
> wrong way
> *****************************************
> // create an array
> errors = new Array()
> // add two items
> errors.push("item 1")
> errors.push("item 2")
> 
> // loop through each item
> for (var i=0;i < errors.length; i++) {
>    trace("removing item")
>    errors.pop()
> }
> // errors.length = 1
> trace("errors.length="+errors.length)
> 
> 
> *****************************************
> right way
> *****************************************
> // create an array
> errors = new Array()
> // add two items
> errors.push("item 1")
> errors.push("item 2")
> var len = errors.length;
> 
> // loop through each item
> for (var i=0;i < len; i++) {
>    trace("removing item")
>    errors.pop()
> }
> // errors.length = 0
> trace("errors.length="+errors.length)
> 
> 
> Look at the condition (i < errors.length).
> 
> If we pop an item off the end of the array then the length of the
array 
> is decreased and we do not iterate through all items in the array.
Hope 
> this helps someone.
> 
> Best Regards,
> Judah
> 
> 
> 
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to