nested array ok
This commit is contained in:
@@ -21,13 +21,14 @@ function array_inspect {
|
||||
}
|
||||
|
||||
function nested_new {
|
||||
local internal
|
||||
ref_name="$1"
|
||||
shift
|
||||
[[ -v nested_count ]] && ((nested_count++)) || nested_count=1
|
||||
nested_name="nested_$nested_count"
|
||||
declare -gA "$nested_name"
|
||||
declare -gn "$ref_name"="$nested_name"
|
||||
local -n internal="$nested_name"
|
||||
declare -n internal="$nested_name"
|
||||
internal['TYPE']='NESTED_ARRAY'
|
||||
local i=0
|
||||
local j=0
|
||||
@@ -38,7 +39,8 @@ function nested_new {
|
||||
}
|
||||
|
||||
function nested_inspect {
|
||||
local -n internal=$1
|
||||
local internal
|
||||
declare -n internal=$1
|
||||
[[ "${internal['TYPE']}" != 'NESTED_ARRAY' ]] && >&2 echo "expected nested array: $1" && return 1
|
||||
|
||||
count=${#internal[@]}
|
||||
@@ -49,12 +51,23 @@ function nested_inspect {
|
||||
array_inspect "${internal[$i]}" " => "
|
||||
done
|
||||
echo ----
|
||||
# ${!internal[@]}; do echo "${key}, ${internal[${key}]}"; done
|
||||
}
|
||||
|
||||
# initialization
|
||||
array_new mine1 'Albert' 20
|
||||
array_new mine2 'Bob' 24
|
||||
array_new mine3 'Cobra' 31 true
|
||||
|
||||
# change values
|
||||
mine1+=(false)
|
||||
: "$mine3" # pseudo use fix shellcheck issue
|
||||
mine3[2]=false
|
||||
|
||||
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
|
||||
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env miaou-bash
|
||||
|
||||
function f1 {
|
||||
if [[ -v nested_count ]]; then
|
||||
>/dev/tty echo already defined $nested_count
|
||||
((nested_count++))
|
||||
else
|
||||
>/dev/tty echo brand new
|
||||
nested_count=1
|
||||
fi
|
||||
echo $nested_count
|
||||
}
|
||||
|
||||
echo "v=$(f1)"
|
||||
echo "v=$(f1)"
|
||||
echo "v=$(f1)"
|
||||
f1
|
||||
f1
|
||||
echo $nested_count
|
||||
Reference in New Issue
Block a user