Actionscript Course

Traversing Arrays

The elements of an Array can easily be parsed with one of these loop instructions: for(), while(), and for each..in.
Traversing an Array with one of these instructions enables you to access each element in an Array, in their order.
Here's an example with each of them:

Using for()

var m_ar:Array = [3, 8, 32];

var nrel:int = m_ar.length;         // get the number of elements in "m_ar"

// define a "for()" loop to traverse the "m_ar" array
for(var i:int=0; i<nrel; i++)
{
  // doubles the current element value
  m_ar[i] *= 2;
}

// uses trace() to check the values in "m_ar"
trace(m_ar);          // 6,16,64
- var i:int=0; - sets the starting value for the index. This index specifies the element from which the for() loop begins to traverse the array (you can set any value for the starting index).
- i<nrel - makes the for() loop to go through to the last element ("nrel" contains the number of items in array).
- i++; - tells the for() instruction to increment the value of "i" by 1, at the end of each loop, to get the next element.

Using while()

var m_ar:Array = [3, 8, 32];

var nrel:int = m_ar.length;         // get the number of elements in "m_ar"
var i:int = 0;                     // defines the starting index

// define a "while()" loop to traverse the "m_ar" array
while(i<nrel)
{
  // doubles the current element value
  m_ar[i] *= 2;
  i++;            // increments the index  to go to the next item
}

// uses trace() to check the values in "m_ar"
trace(m_ar);          // 6,16,64

Using for each..in

var m_ar:Array = [3, 8, 32];

// define a "for each..in" loop to traverse the "m_ar" array
for each(var elm:Number in m_ar)
{
  // doubles the value of the current element, which is stored in "elm"
  elm *= 2;

  // you can perform operations with "elm", its value not affect the array
}

// uses trace() to check the values in "m_ar"
trace(m_ar);          // 3,8,32
- Notice that in this case the array is not affected, "elm" only gets the value of the element, and you can perform operations with it.

Multi-dimensional Array

The element in an array can be another array. In this case we are dealing with a multi-dimensional array.
Let's see an exampe:
// define a simple array (which will be included in the next array)
var arr:Array = [88, 'ActionScript'];

// define an array with 3 elements (the third item is the "arr" variable)
var m_ar:Array = ['tutorials', 'coursesweb.net', arr];

// checks the values of the main array and of the third element
trace(m_ar);          // tutoriale,coursesweb.net,88,ActionScript
trace(m_ar[2]);       // 88,ActionScript
- The array in the third element ("arr" which contains [88, 'ActionScript']) is called "nested array".

Accessing elements in a Nested Array

To access the elements of a nested array, use the following syntax:
array_name[i1][i2]
- "i1" is the index associated to the nested array, "i2" is the index of the element in the nested array.
Example:
// define a simple array (which will be included in the next array)
var arr:Array = [88, 'ActionScript'];

// define an array with 3 elements (the third item is the "arr" variable)
var m_ar:Array = ['tutorials', 'coursesweb.net', arr];

// stores in a variable the second element in the nested array
var test:* = m_ar[2][1];

// check the value of the "test"
trace(test);                  // ActionScript

Traversing a multi-dimensional Array

To traverse a multi-dimensional Array, including the nested arrays, you should use nested loop instructions, like in the following example.
// define a simple array
var arr:Array = [88, 'ActionScript'];

// define an array with 3 elements (the third item is the "arr" variable)
var m_ar:Array = ['tutorials', 'coursesweb.net', arr];

var nrel = m_ar.lengt;              // get the number of elements in "m_ar"

// create 2 nested for() loops to access each item in the multi-dimensional array
for(var i:int=0; i<nrel; i++) {
  var elm:* = m_ar[i];          // get the current element in "m_ar"

  // if "elm" is an Array, define another for() loop to traverse it
  // else, outputs its value
  if(elm is Array) {
    for(var i2:int=0; i2<elm.length; i2++) {
      trace(elm[i2]);           // output the value of the current element in nested array
    }
  }
  else {
    trace(elm);
  }
}
- The "(elm is Array)" returns true if "elm" is an Array data type, else, returns false.
- The first for() loop traverses the main array. If the current element is an array, the nested for() instruction is executed to access its elements (you can use any loop instruction, while(), for..in).
The example above will display in Output panel:
tutorials coursesweb.net
88
ActionScript

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Traverse Arrays - Multi-dimensional Array

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142540)
  2. JavaScript strip_tags and stripslashes (8825)
  3. Date and Time in ActionScript 3 (10086)
  4. Bind Tool and Control Points (4430)
  5. Clear Canvas Context (8116)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)