3 ms·
It's trivial to implement an array in pure shell code without extensions. For example: # append ARRAYNAME VALUE append() { eval N=\"\${${1}_N:-0}\" # r
by wahern 8y ago
It'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.