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