In this very simplified example, I need to address both key and value of an array element:
declare -A writer
writer[H.P.]=Lovecraft
writer[Stephen]=King
writer[Clive]=Barker
writer[Jack]=Ketchum
for i in ${!writer[@]}
do
echo "$i ${writer[$i]}"
done
fullname()
{
pointer=$1[@]
for i in "${!pointer}"
do
echo "? $i"
done
}
fullname writer
The function must display the output in the same format as the example loop before it, and it should receive either array name, list of keys or values, all of which I tried, without success. Any suggestions are greatly appreciated.
Since Bash 4.3, declare
has a flag -n
to define references (this is loosely equivalent to references in C++). This flag tremendously simplifies your problem here:
fullname() {
declare -nl pointer="$1"
for i in "${!pointer[@]}"
do
echo "${pointer[$i]} $i"
done
}
It will be safe if you're having spaces or funny symbols in the keys of your hash (unlike the accepted answer).
indir_keys() {
eval "echo \${!$1[@]}"
}
indir_val() {
eval "echo \${$1[$2]}"
}
fullname()
{
pointer=$1
for i in $(indir_keys $pointer)
do
echo "$i $(indir_val $pointer $i)"
done
}
Gives:
Jack Ketchum
Clive Barker
Stephen King
H.P. Lovecraft
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With