<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.lostsouls.org/w/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Man arrays - Revision history</title>
		<link>http://wiki.lostsouls.org/w/index.php?title=Man_arrays&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.8.2</generator>
		<lastBuildDate>Tue, 05 May 2026 21:39:15 GMT</lastBuildDate>
		<item>
			<title>Laine at 16:36, 11 June 2007</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_arrays&amp;diff=3927&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Description==&lt;br /&gt;
There is support for arrays. The arrays can't be declared, but&lt;br /&gt;
should be allocated dynamically with the function 'allocate()'&lt;br /&gt;
(see efun/allocate).&lt;br /&gt;
&lt;br /&gt;
Arrays are stored by reference, so all assignments of whole&lt;br /&gt;
arrays will just copy the address. The array will be&lt;br /&gt;
deallocated when no variable points to it any longer.&lt;br /&gt;
&lt;br /&gt;
When a variable points to an array, items can be accessed with&lt;br /&gt;
indexing: 'arr[3]' as an example. The name of the array being&lt;br /&gt;
indexed can be any expression, even a function call:&lt;br /&gt;
'func()[2]'. It can also be another array, if this array has&lt;br /&gt;
pointers to arrays:&lt;br /&gt;
&lt;br /&gt;
        arr = allocate(2);&lt;br /&gt;
        arr[0] = allocate(3);&lt;br /&gt;
        arr[1] = allocate(3);&lt;br /&gt;
&lt;br /&gt;
Now 'arr[1][2]' is a valid value.&lt;br /&gt;
&lt;br /&gt;
The 'sizeof()' function (in true C a compiler-directive, not a&lt;br /&gt;
function) will give the number of elements in an array (see&lt;br /&gt;
efun/sizeof).&lt;br /&gt;
&lt;br /&gt;
* NOTE - Nowadays it is most of the time preferable to use an array&lt;br /&gt;
constructor, a list surrounded by '({' and '})',&lt;br /&gt;
e.g. ({ 1, &amp;quot;xx&amp;quot;, 2 }) will construct a new array with size 3,&lt;br /&gt;
initialized with 1, &amp;quot;xx&amp;quot; and 2 respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Operations==&lt;br /&gt;
&lt;br /&gt;
* Indexing&lt;br /&gt;
        &lt;br /&gt;
There are several very useful operations defined on array. &lt;br /&gt;
The most used is the indexing:&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3 });&lt;br /&gt;
                return a[2];      // this will return 2&lt;br /&gt;
&lt;br /&gt;
You also can count from the end of the array. Use &amp;lt;1 to specify&lt;br /&gt;
the last element in the array:&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3 });&lt;br /&gt;
                return a[&amp;lt;3];     // this will return 1&lt;br /&gt;
&lt;br /&gt;
With indexing you can also create sub-arrays:&lt;br /&gt;
                a=({ 0,1,2,3,4,5,6,7 });&lt;br /&gt;
                return a[3..5];   // this will return ({ 3,4,5 })&lt;br /&gt;
                return a[2..&amp;lt;2];  // this will return ({ 2,3,4,5,6 })&lt;br /&gt;
                return a[&amp;lt;5..&amp;lt;3]; // this will return ({ 3,4,5 })&lt;br /&gt;
                return a[&amp;lt;6..5];  // this will return ({ 2,3,4,5 })&lt;br /&gt;
                return a[3..3];   // this will return ({ 3 })&lt;br /&gt;
                return a[3..2];   // this will return ({ })&lt;br /&gt;
                return a[3..0];   // this will return ({ })&lt;br /&gt;
                return a[5..100]; // this will return ({ 5,6,7 })&lt;br /&gt;
                [x..] is interpreted as [x..&amp;lt;1]&lt;br /&gt;
&lt;br /&gt;
* Adding - &lt;br /&gt;
You can add two arrays. The result is one array with the elements&lt;br /&gt;
of both the former arrays:&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1 });&lt;br /&gt;
                b=({ &amp;quot;a&amp;quot;,&amp;quot;b&amp;quot; });&lt;br /&gt;
                return a+b;       // this will return ({ 0,1,&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot; })&lt;br /&gt;
                return b+a;       // this will return ({ &amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,0,1 })&lt;br /&gt;
&lt;br /&gt;
* Subtracting - &lt;br /&gt;
You can erase all elements of one array that occur in another&lt;br /&gt;
array:&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3,4,5,6,7 });&lt;br /&gt;
                b=({ 7,2,5,8,1,9 });&lt;br /&gt;
                return a-b;       // this will return ({ 0,3,4,6 })&lt;br /&gt;
                return b-a;       // this will return ({ 8,9 })&lt;br /&gt;
&lt;br /&gt;
* Interjunction - &lt;br /&gt;
Use the &amp;amp;-operator to create the interjunction of two arrays:&lt;br /&gt;
&lt;br /&gt;
                a=({ 5,2,8,1,9,4 })&lt;br /&gt;
                b=({ 1,6,7,3,4,5 })&lt;br /&gt;
                return a&amp;amp;b;       // this will return ({ 1,4,5 })&lt;br /&gt;
&lt;br /&gt;
* Assigning - &lt;br /&gt;
Assigning can also be done to sub-arrays and is thus very powerful:&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3,4,5,6,7 });&lt;br /&gt;
                a[&amp;lt;4..&amp;lt;3]=({ 8,9 });&lt;br /&gt;
                return a;         // this will return ({ 0,1,2,3,8,9,6,7 })&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3,4,5,6,7 });&lt;br /&gt;
                a[2..5]=({ });&lt;br /&gt;
                return a;         // this will return ({ 0,1,6,7 })&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3,4 });&lt;br /&gt;
                a[3..2]=({ 8,9 });&lt;br /&gt;
                return a;         // this will return ({ 0,1,2,8,9,3,4 })&lt;br /&gt;
&lt;br /&gt;
                a=({ 0,1,2,3,4 });&lt;br /&gt;
                a[3..0]=({ 8,9 });&lt;br /&gt;
                return a;         // this will return ({ 0,1,2,8,9,1,2,3,4 })&lt;br /&gt;
                                  // this is quite funny but true ;-)&lt;br /&gt;
                                  // WARNING: If done unintentionally and&lt;br /&gt;
                                  // within a loop, you can quickly cause&lt;br /&gt;
                                  // the game to run out of memory!&lt;br /&gt;
&lt;br /&gt;
* General - &lt;br /&gt;
Of course for any of the operators explained above you can use&lt;br /&gt;
the combined form of assigning and operating; that means the&lt;br /&gt;
operators +=, -= and &amp;amp;= work.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Tips==&lt;br /&gt;
        &lt;br /&gt;
If you want to make sure that no element is more than once in an&lt;br /&gt;
array you can use the following:&lt;br /&gt;
 &lt;br /&gt;
               a = m_indices(mkmapping(a));&lt;br /&gt;
        &lt;br /&gt;
This creates a mapping out of the array and recreates the array&lt;br /&gt;
at once. The elements in the array can be shuffled by this&lt;br /&gt;
procedure.&lt;/div&gt;</description>
			<pubDate>Mon, 11 Jun 2007 16:36:46 GMT</pubDate>			<dc:creator>Laine</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_arrays</comments>		</item>
	</channel>
</rss>