Arrays: Difference between revisions

Content added Content deleted
m (→‎{{header|Babel}}: Updated to interactive-mode syntax)
Line 578: Line 578:


===Change an array element===
===Change an array element===

Changing a value-array element:


<lang babel>[1 2 3] dup 1 7 set ;
<lang babel>[1 2 3] dup 1 7 set ;
Line 584: Line 586:
{{Out}}
{{Out}}
<pre>[val 0x1 0x7 0x3 ]</pre>
<pre>[val 0x1 0x7 0x3 ]</pre>

Changing a pointer-array element:


<lang babel>[ptr 1 2 3] dup 1 [ptr 7] set ;</lang>
<lang babel>[ptr 1 2 3] dup 1 [ptr 7] set ;</lang>
Line 607: Line 611:
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.
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===
===Convert between arrays and lists===

Convert a value-array to a list of values:


<lang babel>[1 2 3] ar2ls lsnum !</lang>
<lang babel>[1 2 3] ar2ls lsnum !</lang>
Line 613: Line 619:
{{Out}}
{{Out}}
<pre>( 1 2 3 )</pre>
<pre>( 1 2 3 )</pre>

Convert a list of values to a value-array:


<lang babel>(1 2 3) ls2lf ;</lang>
<lang babel>(1 2 3) ls2lf ;</lang>
Line 618: Line 626:
{{Out}}
{{Out}}
<pre>[val 0x1 0x2 0x3 ]</pre>
<pre>[val 0x1 0x2 0x3 ]</pre>

Convert a pointer-array to a list of pointers:


<lang babel>[ptr 'foo' 'bar' 'baz'] ar2ls lsstr !</lang>
<lang babel>[ptr 'foo' 'bar' 'baz'] ar2ls lsstr !</lang>
Line 623: Line 633:
{{Out}}
{{Out}}
<pre>( "foo" "bar" "baz" )</pre>
<pre>( "foo" "bar" "baz" )</pre>

Convert a list of pointers to a pointer-array:


<lang babel>(1 2 3) bons ;</lang>
<lang babel>(1 2 3) bons ;</lang>