constants
This commit is contained in:
+15
-18
@@ -1,10 +1,15 @@
|
||||
#!/usr/bin/env miaou-bash
|
||||
|
||||
PREFIX_NESTED="__nested__"
|
||||
INSPECT_SIMPLE='-> '
|
||||
INSPECT_NESTED='=> '
|
||||
INSPECT_END='~~~~'
|
||||
|
||||
function array_new {
|
||||
ref_name="$1"
|
||||
shift
|
||||
[[ -v nested_count ]] && ((nested_count++)) || nested_count=1
|
||||
nested_name="nested_$nested_count"
|
||||
nested_name="${PREFIX_NESTED}$nested_count"
|
||||
declare -ga "$nested_name"
|
||||
declare -gn "$ref_name"="$nested_name"
|
||||
local -n internal="$nested_name"
|
||||
@@ -18,42 +23,34 @@ function array_inspect {
|
||||
echo "${prefix}array '$1' contains ${#ref[@]} elements"
|
||||
for i in "${ref[@]}"; do
|
||||
if [[ -R $i ]]; then
|
||||
array_inspect "$i" "${prefix} => "
|
||||
array_inspect "$i" "${prefix}${INSPECT_NESTED}"
|
||||
else
|
||||
echo "${prefix}-> $i"
|
||||
echo "${prefix}${INSPECT_SIMPLE}${i}"
|
||||
fi
|
||||
done
|
||||
echo "${prefix}----"
|
||||
echo "${prefix}${INSPECT_END}"
|
||||
}
|
||||
|
||||
# initialization
|
||||
array_new mine1 'Albert' 20
|
||||
array_new mine2 'Bob' 24
|
||||
array_new mine3 'Conrad' 31 true
|
||||
|
||||
array_new mine1 'Albert Dupontel' 20
|
||||
array_new mine2 'Bob Sinclar' 24
|
||||
array_new mine3 'Conrad Ademayor' 31 true
|
||||
array_new nested mine1 mine2 mine3
|
||||
array_inspect nested
|
||||
|
||||
# change values
|
||||
mine1+=(false)
|
||||
: "$mine3" # pseudo use fix shellcheck issue
|
||||
mine3[1]=$((mine3[1] + 8))
|
||||
mine3[2]=false
|
||||
array_inspect nested
|
||||
|
||||
# nested twice
|
||||
declare -n other="${nested[1]:?}" # ':?' fix shellcheck issue
|
||||
: "${other}" # pseudo use fix shellcheck issue
|
||||
array_inspect other
|
||||
array_new mine4 'Darryl' 30
|
||||
other+=(mine4)
|
||||
array_inspect nested
|
||||
|
||||
# nested_new nested1 mine1 mine2 mine3
|
||||
# nested_inspect nested1
|
||||
#
|
||||
# # change nested values
|
||||
# declare -n first="${nested1[0]:?}" # ':?' fix shellcheck issue
|
||||
# first+=("nope")
|
||||
# first[1]=$((first[1] + 5))
|
||||
# nested_inspect nested1
|
||||
# CAN'T PREVENT LOOP!
|
||||
# other+=(nested)
|
||||
# array_inspect nested
|
||||
|
||||
Reference in New Issue
Block a user