CASA Lib ArrayUtil

CASA Lib’s ArrayUtil: Sorting, Searching and Manipulation of Arrays Made Easy

Arrays provide quite a bit of flexibility and functionality when developing in ActionScript 3.0. Having a utility readily available that will allow you sort, search, and manipulate Arrays will save countless hours of coding; along with a lighter script file. This leads me to the next addition of the CASA Lib series post as I breakdown the ArrayUtil.

The ArrayUtil is a very helpful utility for sorting, searching and manipulating Arrays. There are over 20 methods(functions) to this utility and each one provides a very helpful and common functionality. The examples below are very generic and derived from the CASA Lib documentation.

Summary of the ArraryUtil Methods

Randomize the order of the items within an Array

The function randomize() creates a new Array composed of passed Array’s items in a random order.

Example:

var numberArray:Array=new Array(1,2,3,4,5,6,7,8,9,10);
 trace(ArrayUtil.randomize(numberArray));

Remove duplicates within an Array

The function removeDuplicates() creates a new Array of only the non-identical items of passed Array.

Example:

var numberArray:Array=new Array(1,2,3,4,5,4,7,2,9,9);
trace(ArrayUtil.removeDuplicates(numberArray));// Traces 1,3,4,5,7

Remove all instances of an item of an Array

The function removeItem() removes all instances of a specific item of an Array. Also available, removeItems() which removes all of one Array from another Array.

Example:

var numberArray:Array=new Array(1,9,3,4,9,7,2,9,9);
trace("Removed " + ArrayUtil.removeItem(numberArray, 9) + " items.");// Traces Removed 4 items
trace(numberArray);// Traces 1,3,4,7,2

Compare two Arrays to see if they are identical

The function equals() determines if two Arrays contain the same items at the same index.

Example:

var numberArray1:Array=new Array(1,9,3,4,9,7,2,9,9);
var numberArray2:Array=new Array(2,9,3,4,7);
trace(ArrayUtil.equals(numberArray1, numberArray2));// Traces false

Add items of one Array to another Array

The function addItemsAt() modifies the original Array by adding all the items from another Array at a specific position.

Example:

var alphabet:Array=new Array("a","d","e");
var parts:Array=new Array("b","c");

ArrayUtil.addItemsAt(alphabet, parts, 1);

trace(alphabet);// Traces a,b,c,d,e

Fetch a random item of an Array

The function random() returns a random item of an Array.

Example:

var numberArray:Array=new Array(1,2,3,4,5,6,7,8,9,10);
trace(ArrayUtil.random(numberArray));

Determine how many times an Array contains a specific item

The function contains() finds out how many instances of an item an Array contains.

Example:

var numberArray:Array=new Array(1,9,3,4,9,7,2,9,9);
trace("numberArray contains " + ArrayUtil.contains(numberArray, 9) + " 9's."); // Traces 3

Fetch an item within an Array of a specific data type

The function getItemByType() returns the first item of an Array of a specific data type, class, or interface. Also available, getItemsByType() which returns all items that match.

Example:

var numberArray:Array=new Array(1,"a",3,"b",5,"c",7,8,9,10);
trace(ArrayUtil.getItemsByType(numberArray, String));// Traces a,b,c

Determine the average of the values within an Array

The function average() returns the average of the values within an Array.

Example:

var numberArray:Array=new Array(1,2,3,4,5,6,7,8,9,10);
trace(ArrayUtil.average(numberArray));// Traces 5.5

Fetch the highest/lowest value within and Array

The function getHighestValue() finds the highest value of an Array. Also available, getLowestValue() which finds the lowest value of an Array.

Example:

var numberArray:Array=new Array(1,2,20,4,5,6,7,8,9,10);
trace(ArrayUtil.getHighestValue(numberArray));// Traces 20
trace(ArrayUtil.getLowestValue(numberArray));// Traces 1

Sum up the values of an Array

The function sum() adds all of the items of an Array and returns its value.

Example:

var numberArray:Array=new Array(1,2,20,4,5,6,7,8,9,10);
trace(ArrayUtil.sum(numberArray));// Traces 72

Fetch an item of an Array by any key values

The function getItemByAnyKey() the first item that matches the key values on an Object. Also available, getItemsByAnyKey() which returns every item that matches.

Example:

var people:Array=new Array({name:"Aaron",sex:"Male",hair:"Brown"},{name:"Linda",sex:"Female",hair:"Blonde"},{name:"Katie",sex:"Female",hair:"Brown"},{name:"Nikki",sex:"Female",hair:"Blonde"});
var brownOrFemales:Array=ArrayUtil.getItemsByAnyKey(people,{sex:"Female",hair:"Brown"});

for each (var p:Object in brownOrFemales) {
trace(p.name);// Traces Aaron, Linda, Katie, Nikki
}

Determine whether an Array contains all items of another Array

The function containsAll() determines whether an Array contains all items of another Array. Also available, containsAny() which determines whether an Array contains any items of another Array.

Example:

var numberArray:Array=new Array(1,2,3,4,5);
trace(ArrayUtil.containsAll(numberArray, new Array(1, 3, 5)));// Traces true

Summary

As you can see, much of the functionality that this Array utility offers will have a positive impact to the complexity and size of your script while allowing you to focus more on the project at hand. The next post to this series will focus on the a utility, FrameUtil, used for determining label positions and adding and removing frame scripts from the timeline.

2 thoughts on “CASA Lib’s ArrayUtil: Sorting, Searching and Manipulation of Arrays Made Easy

Comments are closed.