4 ms·
Bash is "simpler" in the sense that it doesn't have proper data structures. Recent versions have something like an array, but it's still not quite an array afai
by sbjs 8y ago
Bash is "simpler" in the sense that it doesn't have proper data structures. Recent versions have something like an array, but it's still not quite an array afaik. Bash is ultimately still just a string concatenation language.
- wahern 8y agoIt's trivial to implement an array in pure shell code without extensions. For example: # append ARRAYNAME VALUE append() { eval N=\"\${${1}_N:-0}\" # read [ARRAYNAME]_N N=$(($N + 1)) eval ${1}_${N}=\"\${2}\" # set [ARRAYNAME]_[N] eval ${1}_N=\"\${N}\" # store [ARRAYNAME]_N } It's rather straightforward to implement whatever data structure you want this way.
- dagenix 8y agoWhile I understand your point, looking at that code, the first word into my mind isn't "trivial".
- therein 8y agoMakes me think of this: https://i.imgur.com/1LUysfW.png https://i.imgur.com/1LUysfW.png
- Spivak 8y agoIt actually is once you strip all the bash-isms. Here's what this code is really doing. def append(ARRAYNAME, VALUE): # Store the length of the array as a variable ARRAYNAME_LEN # Store the values of the array as variables ARRAYNAME_0, ARRAYNAME_1,... len = globals.get('{{ ARRAYNAME }}_LEN') || 0 len += 1 # Update the length. globals.set('{{ ARRAYNAME }}_LEN', len) # Add the value. globals.set('{{ ARRAYNAME }}_{{ len }}', VALUE) return Nobody said that bash has pretty syntax but this kind of thing wont even phase you once you're used to all the escaping and bracket substitution rules.
- LukeShu 8y agoBash has had real arrays since 2.0 (1996). Though, the `+=` operator to append to an array wasn't added until 3.1 (2005); before that you had to do `array[${#array[*]}]=word` to append. Since 4.0 (2009) it's also had associative arrays.
- deleted 8y ago[deleted]