Associative Arrays in Siebel Escript, allow us to use strings instead of numbers in the indices of the array. For example, if we have an associative array called "myArray" whereby we store the age of a few contacts, we can fill it with elements as follows:
myArray["Tim"]=25;
myArray["Tom"]=30;
myArray["Jane"]=22;
One advantage of associative arrays is that they can be used in the for...in statement. However we should be careful with a few things which otherwise may result in bugs in our code. These are the following:
Let's say we fill in our associative array, myArray, using a for loop as follows:
for(i=9; i<11; i++)
{
myArray[i] = i*5;
}
After the above code has executed, our array will contain the following:
myArray[9] = 45
myArray[10] = 50
Now, if we use the array in a for...in statement, where let's assume "write" is a function that writes the content of the array in a file on seperate lines:
for( var x in myArray)
{
write(myArray[x]);
}
In the file, we will see,
50
45
In the for...in statement, the index 10 was read before the index 9. This happened because in associative arrays, the indices are treated as strings and hence 10 comes before 9. Therefore, we should be careful in case the sequence in which our elements are read from the associative array is important for our application.
Sorry for digging up old post, but I wonder what's the reason to use "Associative Array" over "Property Set"?
ReplyDeleteWhat's pros and cons of both approach?
the reason is in an associative array you can also add non-Strings which you cannot in a PropertySet
ReplyDelete