Arrays: Difference between revisions
Content added Content deleted
(→{{header|Ecstasy}}: oops the previous edit wasn't supposed to post; I pressed "enter" to put a new line in and it posted it) |
(add RPL) |
||
Line 7,738: | Line 7,738: | ||
/end-free |
/end-free |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|RPL}}== |
|||
Arrays have a predefined size and can only contain floating point numbers. |
|||
They can be created either by enumerating their elements one by one or by creating an array with the same value everywhere: |
|||
[ 1 2 3 4 5 ] |
|||
{ 5 } -1 CON <span style="color:grey">@ create the array [ -1 -1 -1 -1 -1 ]</span> |
|||
To assign a value, you can use either <code>PUT</code> or <code>PUTI</code>. <code>PUT</code> returns only the updated array - other input arguments are gone - whilst <code>PUTI</code> leaves in stack the index, incremented by one : you can then easily assign another value to the following position. |
|||
[ 1 2 3 4 5 ] 3 10 PUT |
|||
returns: |
|||
1: [ 1 2 10 4 5 ] |
|||
but |
|||
[ 1 2 3 4 5 ] 3 10 PUTI |
|||
returns: |
|||
2: [ 1 2 10 4 5 ] |
|||
1: 4 |
|||
Similarly, you can use <code>GET</code> or <code>GETI</code> to retrieve an element. |
|||
[ 10 20 30 40 50 ] 3 GET |
|||
returns: |
|||
1: 30 |
|||
but |
|||
[ 10 20 30 40 50 ] 3 GETI |
|||
returns: |
|||
3: [ 10 20 30 40 50 ] |
|||
2: 4 |
|||
1: 30 |
|||
Another useful data structure in RPL is the list, which is very similar in use to arrays: <code>PUT</code>, <code>PUTI</code>, <code>GET</code> and <code>GETI</code> give the same results. Lists can contain any kind of objects, including lists. Beside direct assignment through <code>PUT</code>, it is also possible to append an element at the beginning or the end of the list with the <code>+</code> operator. |
|||
In recent RPL versions, several functions such as <code>SORT</code> can be applied only to lists, which make this data structure very versatile. The only drawback is the necessity to create a list element by element |
|||
by direct enumeration: |
|||
{ 1 2 3 4 5 } |
|||
by concatenation: |
|||
{ 1 2 3 } { 4 5 } + |
|||
or through a loop: |
|||
{ } 1 5 '''FOR''' j j + '''NEXT''' |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |