docs/pmc/array.pod - Array base class.
This pod file documents the Array base class usage. For implementation details you should look inside the class file, found at src/pmc/array.pmc in the parrot source code.
As with any other PMC,
the following line creates an array PMC in register
You can retrieve the size of the array using
This will put the size of the array in register
In the same way,
assigning an integer directly to the array sets the size of the array.
creates a new Array (with default size 0) and then expands the size of the array to two.
Arrays do not automatically resize themselves when you access out-of-bounds elements. This means that you must remember to size an Array appropriately before storing anything in it.
Elements are accessed using indexes, as in any programming language.
The following code initializes an array in
P0 with size two,
and sets the first position to the integer
-8 and second position to the floating point number
You can also assign directly from registers; for instance:
leaves P0 in the same state as in the previous code snippet.
To retrieve elements, we use the same syntax:
Those two lines retrieve the values from the array back into registers.
The value stored at a given position is not fixed; it can be changed simply by assigning a new value:
Accessing an out-of-bounds array element raises an exception; if you want an Array that will automatically resize,
then use a
You can test if there is a defined element at an array position by using
for the position you want to test.
On the other hand,
if you only want to test whether a given element exists (rather than whether it is defined),
then use the
exists op instead:
Explain a little more which exception will be raised in case you access a out-of-bounds index on the array (as soon we have exceptions).