I've decided to start a little series of articles on the blog called Core AS3 that will deal with small, simple snippets of code (<5 lines) that are very useful but not particularly self-evident. I often encounter questions at workshops and conferences about small coding constructs that I take for granted, and it occurred to me it might be helpful to document and share them with the community.

The first article in this series is about modulus. Modulus, which is represented by the percent sign % simply returns the remainder of a division operation. For example, 21 modulus 5 is 1. This is handy for a lot of different things, but I'm going to focus on four of them: alternation, frequency, wrapping, and two dimensional data sets.

All code will be simplified to focus on the concept at hand. Most of it should also be applicable to any similar language (ActionScript 2, ActionScript 3, JavaScript, etc)

Using Modulus for Alternation
This is perhaps the simplest use case. Say you want to alternate row colors in a list - you can simply make the color of each row a function of the modulus value of the rowcount:

if (rowIndex % 2 == 0) {
   rowColor = 0xFFFFFF;
} else {
   rowColor = 0xCCCCCC;
}

Using Modulus to Vary Frequency
Similar to the above, you can use modulus to vary the frequency of execution of a block of code within a repeating block. For example:

// tick runs every frame:
function tick() {
   trace("I run every frame");
   if (++frameCount % 10 == 0) {
      trace("I run every 10 frames");
    }
}

Using Modulus to Wrap Values
You can also modulus to wrap a value within a specified range. For example, if you want to a sprite to move right, but wrap back to the left side of the screen when it hits the right side, you could use this:

sprite.x = (sprite.x + 5) % stage.stageWidth;
It's easy to modify this to work with negative x motion as well, you simply have to ensure the value is always positive:
sprite.x = (sprite.x + velX + stage. stageWidth) % stage. stageWidth;

Using Modulus to Flatten Two Dimensional Data Sets
Creating two dimensional arrays by nesting arrays is very inefficient. It's much better to flatten the 2D data into a single array. You can do this easily using modulus and simple multiplication:

// iterate the array:
function scanArray() {
   for (var i=0; i<flatArray.length; i++) {
      var x = i%columns;
      var y = Math.floor(i/columns);
      trace("value at x="+x+",y="+y+" is '"+flatArray[i]+"'");
    }
}

// access individual values:
function getValueAt(x,y) {
   return flatArray[x+y*columns];
}
Based on some feedback in the comments below, I did some additional testing, and found that this technique, while novel, is actually slower than using nested arrays in the latest player 9. Based on this, my recommendations would be to use flattened arrays for performance critical routines in AS1/2, but stick with the more readable and performant nested approach for everything else.

Have other handy uses for modulus? Post them in the comments below.