blob: 883dddb02b9f7fe4facf4fd1dff5c4d0ba1e2129 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001CONCEPT
2 arrays
3
4DESCRIPTION
5 There is support for arrays. The arrays can't be declared, but
6 should be allocated dynamically with the function 'allocate()'
7 (see efun/allocate).
8
9 Arrays are stored by reference, so all assignments of whole
10 arrays will just copy the address. The array will be
11 deallocated when no variable points to it any longer.
12
13 When a variable points to an array, items can be accessed with
14 indexing: 'arr[3]' as an example. The name of the array being
15 indexed can be any expression, even a function call:
16 'func()[2]'. It can also be another array, if this array has
17 pointers to arrays:
18
19 arr = allocate(2);
20 arr[0] = allocate(3);
21 arr[1] = allocate(3);
22
23 Now 'arr[1][2]' is a valid value.
24
25 The 'sizeof()' function (in true C a compiler-directive, not a
26 function) will give the number of elements in an array (see
27 efun/sizeof).
28
29NOTE
30 Nowadays it is most of the time preferable to use an array
31 constructor, a list surrounded by '({' and '})',
32 e.g. ({ 1, "xx", 2 }) will construct a new array with size 3,
33 initialized with 1, "xx" and 2 respectively.
34
35
36OPERATIONS
37
38 INDEXING
39 There are several very useful operations defined on arrays.
40 The most used is the indexing:
41 a=({ 0,1,2,3 });
42 return a[2]; // this will return 2
43 You also can count from the end of the array. Use <1 to specify
44 the last element in the array:
45 a=({ 0,1,2,3 });
46 return a[<3]; // this will return 1
47 With indexing you can also create sub-arrays:
48 a=({ 0,1,2,3,4,5,6,7 });
49 return a[3..5]; // this will return ({ 3,4,5 })
50 return a[2..<2]; // this will return ({ 2,3,4,5,6 })
51 return a[<5..<3]; // this will return ({ 3,4,5 })
52 return a[<6..5]; // this will return ({ 2,3,4,5 })
53 return a[3..3]; // this will return ({ 3 })
54 return a[3..2]; // this will return ({ })
55 return a[3..0]; // this will return ({ })
56 return a[5..100]; // this will return ({ 5,6,7 })
57 [x..] is interpreted as [x..<1]
58
59 ADDING
60 You can add two arrays. The result is one array with the elements
61 of both the former arrays:
62 a=({ 0,1 });
63 b=({ "a","b" });
64 return a+b; // this will return ({ 0,1,"a","b" })
65 return b+a; // this will return ({ "a","b",0,1 })
66
67 SUBTRACTING
68 You can erase all elements of one array that occur in another
69 array:
70 a=({ 0,1,2,3,4,5,6,7 });
71 b=({ 7,2,5,8,1,9 });
72 return a-b; // this will return ({ 0,3,4,6 })
73 return b-a; // this will return ({ 8,9 })
74
75 INTERJUNCTION
76 Use the &-operator to create the interjunction of two arrays:
77 a=({ 5,2,8,1,9,4 })
78 b=({ 1,6,7,3,4,5 })
79 return a&b; // this will return ({ 1,4,5 })
80
81 ASSIGNING
82 Assigning can also be done to sub-arrays and is thus very powerful:
83 a=({ 0,1,2,3,4,5,6,7 });
84 a[<4..<3]=({ 8,9 });
85 return a; // this will return ({ 0,1,2,3,8,9,6,7 })
86
87 a=({ 0,1,2,3,4,5,6,7 });
88 a[2..5]=({ });
89 return a; // this will return ({ 0,1,6,7 })
90
91 a=({ 0,1,2,3,4 });
92 a[3..2]=({ 8,9 });
93 return a; // this will return ({ 0,1,2,8,9,3,4 })
94
95 a=({ 0,1,2,3,4 });
96 a[3..0]=({ 8,9 });
97 return a; // this will return ({ 0,1,2,8,9,1,2,3,4 })
98 // this is quite funny but true ;-)
99 // WARNING: If done unintentionally and
100 // within a loop, you can quickly cause
101 // the game to run out of memory!
102
103 GENERAL
104 Of course for any of the operators explained above you can use
105 the combined form of assigning and operating; that means the
106 operators +=, -= and &= work.
107
108
109TIPS
110 If you want to make sure that no element is more than once in an
111 array you can use the following:
112 a = m_indices(mkmapping(a));
113 This creates a mapping out of the array and recreates the array
114 at once. The elements in the array can be shuffled by this
115 procedure.
116