foreach

  • fotbon
  • Novice
  • Novice
  • fotbon
  • Posts: 20

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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.
  • fotbon
  • Novice
  • Novice
  • fotbon
  • Posts: 20

Post 3+ Months Ago

rockin and rollin, works fab =)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 16 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.