Arrays: Difference between revisions
Content deleted Content added
m →{{header|Babel}}: Updated to interactive-mode syntax |
|||
Line 563:
===Create an array===
There are two kinds of array in Babel: value-arrays and pointer-arrays. A value-array is a flat array of data words. A pointer-array is an array of pointers to other things (including value-arrays). You can create a data-array with plain square-brackets. You can create a value-array with the [ptr ] list form:
<lang babel>[val 1 2 3]</lang>▼
<lang babel>[ptr 1 2 3]</lang>
===Get a single array element===
<lang babel>[
{{Out}}
<pre>[val 0x2 ]</pre>
===Change an array element===
<lang babel>[
</lang>▼
{{Out}}
<pre>[val 0x1 0x7 0x3 ]</pre>
<lang babel>[ptr 1 2 3] dup 1 [ptr 7]
{{Out}}
<pre>[ptr [val 0x1 ] [val 0x7 ] [val 0x3 ] ]</pre>
===Select a range of an array===
<lang babel>[ptr
{{Out}}
<pre>[ptr [val 0x2 ] [val 0x3 ] ]</pre>
===Add a new element to an array===
You can concatenate arrays of same type:
<lang babel>[1 2 3] [4] cat</lang>
<lang babel>[ptr 1 2 3] [ptr 4] cat</lang>
Concatenation creates a new array - it does not add to an array in-place. Instead, Babel provides operators and standard utilities for converting an array to a list in order to manipulate it, and then convert back.
===Converting between arrays and lists===
▲</lang>
<lang babel>[1 2 3] ar2ls lsnum !</lang>
{{Out}}
<pre>( 1 2 3 )</pre>
<lang babel>(1 2 3) ls2lf ;</lang>
{{Out}}
<pre>[val 0x1 0x2 0x3 ]</pre>
<lang babel>[ptr 'foo' 'bar' 'baz'] ar2ls lsstr !</lang>
{{Out}}
<pre>( "foo" "bar" "baz" )</pre>
<lang babel>(1 2 3) bons ;</lang>
{{Out}}
<pre>[ptr [val 0x1 ] [val 0x2 ] [val 0x3 ] ]</pre>
To learn more about manipulating arrays and lists in Babel, type "help !" (no quotes) and follow the instructions to load the man.sp file.
=={{header|BASIC}}==
|