17.7. Arrays in WMLScript

Unlike programming/scripting languages such as C++, Java and JavaScript, WMLScript does not support conventional arrays. However, the string data type and the five functions in the String standard library for manipulating elements (elements(), elementAt(), insertAt(), removeAt() and replaceAt()) can help us simulate the behavior of arrays. In the following sections, we will see how to use them to perform array operations.


17.7.1. Declaration of Arrays

An empty string can be used to represent an empty array. Hence, to declare a new array, just use the usual way to declare a WMLScript variable and initialize it with an empty string, like this:


var array = "";


17.7.2. Assigning Elements to an Array

After declaring a new array, we can assign some elements to it. To assign elements to an array, we can use the insertAt() function of the String standard library. Below is an example demonstrating how to do this:


array = String.insertAt(array, "WAP Tutorial", 0, ",");
array = String.insertAt(array, "WML Tutorial", 1, ",");
array = String.insertAt(array, "WMLScript Tutorial", 2, ",");


We assign three elements to the array using the above lines of script. After executing the script, the array elements at index 0, 1 and 2 become "WAP Tutorial", "WML Tutorial" and "WMLScript Tutorial" respectively.

Notice that we choose a character that does not exist in the array elements as the delimiter. The same delimiter has to be used throughout the lifetime of the array. For example, we have used a comma as the delimiter in the previous example. Later when we want to retrieve an element from the array, we have to use the same delimiter.


17.7.3. Getting Elements of an Array

To get an element of an array, we need to use the elementAt() function of the String standard library. Below is an example demonstrating how to get the third element (index = 2) of the earlier array variable:


var third_element = String.elementAt(array, 2, ",");


After the execution of the above line of script, the third_element variable contains the value "WMLScript Tutorial".

Below shows another example. We use a for loop to retrieve all elements in the array:


var message = "The elements in the array are: ";
for (var i=0; i<String.elements(array, ","); i++)
  message += "(at index " + i + ") " + String.elementAt(array, i, ",") + " ";


In the above script, "String.elements(array)" is used to find the number of elements in the array.

After the execution, the message variable contains the string value "The elements in the array are: (at index 0) WAP Tutorial (at index 1) WML Tutorial (at index 2) WMLScript Tutorial ".


17.7.4. Changing the Value of an Array Element

The replaceAt() function of the String standard library can help us change the value of an array element. The following example script demonstrates how to do this. It adds parentheses to each element of the array variable.


var temp_str;
for (var i=0; i<String.elements(array, ","); i++){
  temp_str = "(" + String.elementAt(array, i, ",") + ")";
  array = String.replaceAt(array, temp_str, i, ",");
}


Inside the for loop, we first retrieve the value of an element using the elementAt() function. Then we add the "(" and ")" characters to the beginning and the end of the element value, and assign the result back to the original position in the array. After the execution of the above script, the array variable contains the following three array elements:


Previous Page Page 51 of 71 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page