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:
(WAP Tutorial)
(WML Tutorial)
(WMLScript Tutorial)
Previous Page | Page 51 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data