Accessing a value
Access by indices
Each element of an array may be accessed by an integer key.
Examples:
a[1]
a[x]
a[10 + x / 2]
The indice may be any expression that evaluate to an integer number.
The expression can't embed another indexing.
For example:
a[10 + b[5]] ... is not valid
The indice used to retrieve an element is not necessary the one used when
entering the data with an indice. Several functions or operations can have
the effect of reordering or renumbering the elements...
Scriptol allows to scan an array and access a value without to know its integer
key, thanks to the empty indice [] that returns the currently pointed out
element.
Example:
scan a
print a[]
a[] = "read"
/scan
The scan control structure points out each element successively and the []
indice designates the current one.
Since arrays may contains any kind of object, and even other arrays, you need
for a dynamic variable to get an element:
dyn x = a[1]
If you know the type of items, you can assign static variables:
text t = a[2]
int i = a[3]
We can also scan the array by the integrated iterator.
Access by iterator
The iterator is a set of methods of the list object to access sequentially
its elements, in ascending or descending order:
begin() | points out the first element and returns it. |
end() | points out the last element and returns it. |
inc() | moves to the next element. Return the value, or nil beyond the last element. |
dec() | moves to the previous element. Returns the value or nil. |
index() | returns the index of the pointed out element. |
value() | returns the value of the pointed out element. |
[] | empty indice for the current element. |
nil | means for "not in list" and is returned by various functions when no value can be returned. |
Example of iteration | array
a = (1,2,3) a.begin() ` move to the first element while a[] <> nil ` test if end of list reached print a[] ` print the currently pointed out element a.inc() ` move to next element /while |
>
1 > 2 > 3 |
The array may be displayed in reverse order, by replacing:
- a.begin() by a.end()
- a.inc() by a.dec()
Exercises |
1) An array is defined: |