foreach

  • fotbon
  • Novice
  • Novice
  • No Avatar
  • Joined: Mar 16, 2004
  • Posts: 20
  • Status: Offline

Post September 10th, 2004, 5:41 pm

Is there any command that works as php's foreach in actionscript?

I need to cycle through a non-consecutive number array to see if any of the values are greater than zero, but I don't know how to do this without wasting massive cycles checking the undefined parts of the array.

Example:

array[0] = 5;
array[3] = 8;
array[8] = 1;
array[9] = 0;
array[15] = 0;
array[19] = 20;

How would I check only the defined parts of the arrays to see if they're defined as greater than 0?

[/blur][/flash][/code]
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post September 10th, 2004, 5:41 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post September 11th, 2004, 1:06 am

Using a for..in loop.
Code: [ Select ]
myArray = [];
myArray[0] = 1;
myArray[5] = 0;
myArray[3] = 3;
myArray[7] = 7;
for(var item in myArray){
    trace("index: " + item + (myArray[item] > 0 ? " > " : " < ") + "0");
}
  1. myArray = [];
  2. myArray[0] = 1;
  3. myArray[5] = 0;
  4. myArray[3] = 3;
  5. myArray[7] = 7;
  6. for(var item in myArray){
  7.     trace("index: " + item + (myArray[item] > 0 ? " > " : " < ") + "0");
  8. }

for..in loops access the elements in reverse order of which they are assigned, the above will trace the order 7,3,5,0. They only evaluate that which has been assigned :)
Using any method on the array (reverse, sort, ect..) before the loop will taint the outcome of a for..in loop by assigning an "undefined" value to each null item in the array, which is then visible to the for..in loop causing it to evaluate the undefineds.
Strong with this one, the sudo is.
  • fotbon
  • Novice
  • Novice
  • No Avatar
  • Joined: Mar 16, 2004
  • Posts: 20
  • Status: Offline

Post September 11th, 2004, 9:24 pm

rockin and rollin, works fab =)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 27 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.