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 to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Traverse Arrays - Multi-dimensional Array

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)