github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/parser/item-index.md (about) 1 # `[ Index ]` 2 3 > Outputs an element from an array, map or table 4 5 ## Description 6 7 Outputs an element or multiple elements from an array, map or table. 8 9 Please note that indexes in Murex are counted from zero. 10 11 ## Usage 12 13 ``` 14 <stdin> -> [ element ] -> <stdout> 15 $variable[ element ] -> <stdout> 16 17 <stdin> -> ![ element ] -> <stdout> 18 ``` 19 20 ## Examples 21 22 Return the 2nd (1), 4th (3) and 6th (5) element in an array: 23 24 ``` 25 » ja [0..9] -> [ 1 3 5 ] 26 [ 27 "1", 28 "3", 29 "5" 30 ] 31 ``` 32 33 Return the data-type and description of **config shell syntax-highlighting**: 34 35 ``` 36 » config -> [[ /shell/syntax-highlighting ]] -> [ Data-Type Description ] 37 [ 38 "bool", 39 "Syntax highlighting of murex code when in the interactive shell" 40 ] 41 ``` 42 43 Return all elements _except_ for 1 (2nd), 3 (4th) and 5 (6th): 44 45 ``` 46 » a [0..9]-> ![ 1 3 5 ] 47 0 48 2 49 4 50 6 51 7 52 8 53 9 54 ``` 55 56 Return all elements except for the data-type and description: 57 58 ``` 59 » config -> [[ /shell/syntax-highlighting ]] -> ![ Data-Type Description ] 60 { 61 "Default": true, 62 "Dynamic": false, 63 "Global": true, 64 "Value": true 65 } 66 ``` 67 68 Return the top 5 processes from `ps`, ordered by memory usage: 69 70 ``` 71 » ps aux -> [PID %MEM COMMAND] -> sort -nrk2 -> [..5] 72 915961 14.4 /home/lau/dev/go/bin/gopls 73 916184 4.4 /opt/visual-studio-code/code 74 108025 2.9 /usr/lib/firefox/firefox 75 1036 2.4 /usr/lib/baloo_file 76 915710 1.9 /opt/visual-studio-code/code 77 ``` 78 79 Return the 1st and 30th row: 80 81 ``` 82 » ps aux -> [*1 *30] 83 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 84 root 37 0.0 0.0 0 0 ? I< Dec18 0:00 [kworker/3:0H-events_highpri] 85 ``` 86 87 Return the 1st and 5th column: 88 89 ``` 90 » ps aux -> [*A *E] -> head -n5 91 USER VSZ 92 root 168284 93 root 0 94 root 0 95 root 0 96 ``` 97 98 ## Detail 99 100 ### Index counts from zero 101 102 Indexes in Murex behave like any other computer array in that all arrays 103 start from zero (`0`). 104 105 ### Include vs exclude 106 107 As demonstrated in the examples above, `[` specifies elements to include 108 where as `![` specifies elements to exclude. 109 110 ### Don't error upon missing elements 111 112 By default, **index** generates an error if an element doesn't exist. However 113 you can disable this behavior in `config` 114 115 ``` 116 » config -> [ foobar ] 117 Error in `[` ((builtin) 2,11): Key 'foobar' not found 118 119 » config set index silent true 120 121 » config -> [ foobar ] 122 ``` 123 124 ## Synonyms 125 126 * `[` 127 * `![` 128 * `item-index` 129 * `index` 130 131 132 ## See Also 133 134 * [`[ ..Range ]`](../parser/range.md): 135 Outputs a ranged subset of data from STDIN 136 * [`[[ Element ]]`](../parser/element.md): 137 Outputs an element from a nested structure 138 * [`a` (mkarray)](../commands/a.md): 139 A sophisticated yet simple way to build an array or list 140 * [`config`](../commands/config.md): 141 Query or define Murex runtime settings 142 * [`count`](../commands/count.md): 143 Count items in a map, list or array 144 * [`ja` (mkarray)](../commands/ja.md): 145 A sophisticated yet simply way to build a JSON array 146 * [`mtac`](../commands/mtac.md): 147 Reverse the order of an array 148 149 <hr/> 150 151 This document was generated from [builtins/core/index/index_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/index/index_doc.yaml).