Browse Source

nested array ok

main
pvincent 6 days ago
parent
commit
7fb48df8a8
  1. 19
      test/associative.test.bash
  2. 19
      test/increment.test.bash

19
test/associative.test.bash

@ -21,13 +21,14 @@ function array_inspect {
} }
function nested_new { function nested_new {
local internal
ref_name="$1" ref_name="$1"
shift shift
[[ -v nested_count ]] && ((nested_count++)) || nested_count=1 [[ -v nested_count ]] && ((nested_count++)) || nested_count=1
nested_name="nested_$nested_count" nested_name="nested_$nested_count"
declare -gA "$nested_name" declare -gA "$nested_name"
declare -gn "$ref_name"="$nested_name" declare -gn "$ref_name"="$nested_name"
local -n internal="$nested_name"
declare -n internal="$nested_name"
internal['TYPE']='NESTED_ARRAY' internal['TYPE']='NESTED_ARRAY'
local i=0 local i=0
local j=0 local j=0
@ -38,7 +39,8 @@ function nested_new {
} }
function nested_inspect { 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 [[ "${internal['TYPE']}" != 'NESTED_ARRAY' ]] && >&2 echo "expected nested array: $1" && return 1
count=${#internal[@]} count=${#internal[@]}
@ -49,12 +51,23 @@ function nested_inspect {
array_inspect "${internal[$i]}" " => " array_inspect "${internal[$i]}" " => "
done done
echo ---- echo ----
# ${!internal[@]}; do echo "${key}, ${internal[${key}]}"; done
} }
# initialization
array_new mine1 'Albert' 20 array_new mine1 'Albert' 20
array_new mine2 'Bob' 24 array_new mine2 'Bob' 24
array_new mine3 'Cobra' 31 true 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_new nested1 mine1 mine2 mine3
nested_inspect nested1 nested_inspect nested1
# change nested values
declare -n first="${nested1[0]:?}" # ':?' fix shellcheck issue
first+=("nope")
first[1]=$((first[1] + 5))
nested_inspect nested1

19
test/increment.test.bash

@ -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
Loading…
Cancel
Save