MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | SYNOPSIS |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 2 | mixed arr[index] |
| 3 | int str[index] |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 4 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 5 | mixed * arr[from .. to] |
| 6 | string str[from .. to] |
| 7 | mixed m[key, from .. to] |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 8 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 9 | DESCRIPTION |
| 10 | Return one element from a string/array (first form), or a slice |
| 11 | (substring resp. subarray) of a string/array/mapping (second form). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 12 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 13 | The indexes <index>, <from> and <to> are numbered 0 to |
| 14 | strlen(str)-1, sizeof(arr)-1 resp. widthof(m)-1. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 15 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 16 | If an index is written '<value', the value is counted from the |
| 17 | end of the string/array/mapping and is numbered 1 to strlen(str), |
| 18 | sizeof(arr) resp. widthof(m). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 19 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 20 | If an index is written '>value', the value is counted from the |
| 21 | end of the string/array/mapping if it is negative (starting with |
| 22 | -1 for the last element), and from the beginning if it is positive |
| 23 | (starting with 0 for the first element). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 24 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 25 | If <from> is omitted, it defaults to the beginning of the |
| 26 | string/array/mapping. |
| 27 | If <to> is omitted, it defaults to the beginning of the |
| 28 | string/array/mapping. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 29 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 30 | In the first form, the <index> must be within the bounds of |
| 31 | the string/array, or a runtime error occurs. |
| 32 | In the second form, the indexes will be fitted to the bounds of |
| 33 | the string/array/mapping. If <from> is greater than <to>, or both |
| 34 | outside the bounds, an empty string/array ("" resp. ({})) will |
| 35 | be returned. |
| 36 | |
| 37 | The closure notation is straightforward: |
| 38 | |
| 39 | [index] -> ({'#[, arr, index }) |
| 40 | [<index] -> ({'#[<, arr, index }) |
| 41 | [>index] -> ({'#[>, arr, index }) |
| 42 | [from..to] -> ({'#[..], arr, from, to }) |
| 43 | [<from..to] -> ({'#[<..], arr, from, to }) |
| 44 | [from..<to] -> ({'#[..<], arr, from, to }) |
| 45 | [<from..<to] -> ({'#[<..<], arr, from, to }) |
| 46 | [>from..to] -> ({'#[>..], arr, from, to }) |
| 47 | [from..>to] -> ({'#[..>], arr, from, to }) |
| 48 | [>from..<to] -> ({'#[>..<], arr, from, to }) |
| 49 | [<from..>to] -> ({'#[<..>], arr, from, to }) |
| 50 | [>from..>to] -> ({'#[>..>], arr, from, to }) |
| 51 | [key,index] -> ({'#[,], m, index }) |
| 52 | [key,<index] -> ({'#[,<], m, index }) |
| 53 | [key,>index] -> ({'#[,>], m, index }) |
| 54 | [key,from..to] -> ({'#[,..], m, from, to }) |
| 55 | [key,<from..to] -> ({'#[,<..], m, from, to }) |
| 56 | [key,from..<to] -> ({'#[,..<], m, from, to }) |
| 57 | [key,<from..<to] -> ({'#[,<..<], m, from, to }) |
| 58 | [key,>from..to] -> ({'#[,>..], m, from, to }) |
| 59 | [key,from..>to] -> ({'#[,..>], m, from, to }) |
| 60 | [key,>from..<to] -> ({'#[,>..<], m, from, to }) |
| 61 | [key,<from..>to] -> ({'#[,<..>], m, from, to }) |
| 62 | [key,>from..>to] -> ({'#[,>..>], m, from, to }) |
| 63 | |
| 64 | EXAMPLES |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 65 | foo = ({ 1, 2, 3, 4 }); str = "test"; |
| 66 | |
| 67 | foo[1] -> 1 str[1] -> 'e' == 101 |
| 68 | foo[1..2] -> ({ 2, 3 }) str[1..2] -> "es" |
| 69 | foo[2..1] -> ({ }) str[2..1] -> "" |
| 70 | foo[0..<2] -> ({ 1, 2 }) str[0..<2] -> "tes" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 71 | foo[..<2] -> ({ 1, 2 }) str[..<2] -> "tes" |
| 72 | foo[<3..] -> ({ 2, 3, 4 }) str[<3..] -> "est" |
| 73 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 74 | foo[1] = 5 -> foo == ({ 1, 5, 3, 4 }) |
| 75 | foo[1..2] = ({ 5, 6, 7 }) -> foo == ({ 1, 5, 6, 7, 4 }) |
| 76 | foo[1..2] = ({ }) -> foo == ({ 1, 4 }) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 77 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 78 | str[1] = 'a' -> str == "tast" |
| 79 | str[1..2] = "bar" -> str == "tbart" |
| 80 | str[1..2] = "" -> str == "tt" |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 81 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 82 | HISTORY |
| 83 | slice_array() is the old form of the [] operator on arrays. |
| 84 | extract() is the old form of the [] operator on strings. |
| 85 | Both ARE NO LONGER SUPPORTED and should not be used anymore! |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 86 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 87 | The syntax for ``counting from last element'' has changed |
| 88 | between versions 3.1.J and 3.1.K from ``-1'' to ``<1''. |
| 89 | foo[0..-1] is now an empty string resp. array. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 90 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 91 | LDMud 3.3 introduced the '>' indexing method. |
| 92 | |
| 93 | LDMud 3.6.6 introduced range indexing for mappings. |
| 94 | |
| 95 | SEE ALSO |
| 96 | member(E), sizeof(E) |
| 97 | |