From RTSC-Pedia

Jump to: navigation, search
revision tip
—— LANDSCAPE orientation
[printable version]  [offline version]offline version generated on 04-Aug-2010 21:08 UTC

XDCscript - Map-Object

An ordered hash table type

Contents

Synopsis

 
 
 
 
 
 
 
 
 
 
var map = someMapObj;
 
map.length;           // Gets the number of values in this map. 
 
var someVar = map[i]; // Gets an element of this map whose index i is in the range 0..length-1. 
map[i] = someValue;   // Sets an element of this map whose index i is in the range 0..length-1. 
 
map[s] = someValue;   // Sets a map element given a string-value key s. 
var someVar = map[s]; // Gets a map element given a string-value key s. 
                      // Returns undefined if the designated element does not exist. 

Description

A map is an aggregate that comprises an expandable ordered set of values of some common base type, indexed by strings as well as non-negative integers. Maps are a constrained hybrid of a JavaScript object and array; each map has a length property and map objects can be indexed either by a numeric index or by a string valued key.

Because of JavaScript's type conversion rules, it is not possible to distinguish between map["2"] and map[2]; strings that represent integral numbers are converted to numbers before accessing a map element's value. So, all string keys used to defined elements in a map must be non-numeric.

Unlike JavaScript arrays, maps range check indexes; attempts to index outside the range of defined values results in an error.

 
 
 
 
 map = [["zero", 0]];
 print(map["one"]);  // prints undefined
 print(map[1]);      // ERROR: map has just one element
 print(map["1"]);    // ERROR: same as above

Examples

Iterating over the elements of a map

Iterating over the elements of a map

 
 
 
 
 
 
 
 
 
/* display elements of map by index */
for (var i = 0; i < Mod.map.length; i++) {
    print("map[" + i + "] = " + Mod.map[i]);
}
 
/* display elements of map by name */
for (var i in Mod.map) {
    print("map{" + i + "} = " + Mod.map[i]);
}

When iterating of the elements of a map by name, the order is always the same as iterating by index starting from 0.

Deleting a map

Deleting a map

 
Mod.map = undefined;
Redefining a map

Redefining a map

 
 
 
 
Mod.MAP = [     /* redefine a map */
    ["one", 1],
    ["two", 2]
];
Deleting elements from a map

Deleting elements from a map

 
delete Mod.map["two"]; /* delete the "two" element in the map */
Adding elements to a map

Adding elements to a map.  The special $putHead() and $putTail() operations will add a name-value pair to the head or tail or a map, respectively.

 
 
 
 
 
 
if (!"zero" in Mod.map) {
    Mod.MAP.$putHead("zero", 0);       /* add new ["zero", 0] to front of map */
}
if (!"zero" in Mod.map) {
    Mod.MAP.$putTail("zero", 0);       /* add new ["zero', 0] to end of map */
}

The special $putHead() and $putTail() operations will move existing elements to the head or tail, respectively.

 
 
 
 
/* reverse the elements of a map */
for (var k in Mod.map) {
    Mod.map.$putHead(k, Mod.map[k]);/* move existing element to front of map */
}
Replacing elements in a map

Replacing elements in a map

 
 
Mod.map["two"] = 1; /* redefine the "two" element */
Mod.map[1] = "one"; /* redefine the second element element */
[printable version]  [offline version]offline version generated on 04-Aug-2010 21:08 UTC
Copyright © 2008 The Eclipse Foundation. All Rights Reserved


Views
Personal tools
package reference