Browse Source

nested array 4

main
pvincent 6 days ago
parent
commit
9566dc6d33
  1. 72
      test/associative.test.bash

72
test/associative.test.bash

@ -1,33 +1,65 @@
#!/usr/bin/env miaou-bash #!/usr/bin/env miaou-bash
# declare -a normal
# declare -n ref1=normal
# ref1+=(5)
# ref1+=(9)
# ref1+=(10)
# declare -p normal
# declare -p ref1
# for i in "${ref1[@]}"; do echo "-> $i"; done
function array_new { function array_new {
ref_name="$1" ref_name="$1"
shift shift
[[ -v nested_count ]] && ((++nested_count)) || nested_count=1
nested_name="_nested_$nested_count"
[[ -v nested_count ]] && ((nested_count++)) || nested_count=1
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"
declare -n internal="$nested_name"
local -n internal="$nested_name"
internal+=("$@") internal+=("$@")
} }
function array_inspect { function array_inspect {
declare -n ref="$1"
echo "array '$1' contains ${#ref[@]} elements"
for i in "${ref[@]}"; do echo "-> $i"; done
echo -----
local -n ref="$1"
prefix="${2:-}"
echo "${prefix}array '$1' contains ${#ref[@]} elements"
local i
for i in "${ref[@]}"; do echo "${prefix}-> $i"; done
echo "${prefix}----"
}
function nested_new {
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"
internal['TYPE']='NESTED_ARRAY'
# internal['VALUES']=("$@")
} }
array_new mine1 'toto est fluo' 0
array_inspect mine1
mine1+=("B C D" "E")
array_inspect mine1
function nested_inspect {
local -n internal=$1
[[ "${internal['TYPE']}" != 'NESTED_ARRAY' ]] && >&2 echo "expected nested array: $1" && return 1
count=${#internal[@]}
count=$((count - 1))
echo "nested '$1' contains $count arrays"
local i
for ((i = 0; i < count; i++)); do
array_inspect "${internal[$i]}" " => "
done
echo ----
# ${!internal[@]}; do echo "${key}, ${internal[${key}]}"; done
}
array_new mine1 'Albert' 20
array_new mine2 'Bob' 24
array_new mine3 'Cobra' 31 true
# array_inspect mine1
# array_inspect mine2
# array_inspect mine3 " => "
declare -A nested
nested['TYPE']='NESTED_ARRAY'
nested[0]=mine1
nested[1]=mine2
nested[2]=mine3
nested_inspect nested
nested_new nested1 mine1 mine2 mine3
# nested_inspect nested1
Loading…
Cancel
Save