Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash indirect reference to an associative array

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.

like image 201
Ulrik Avatar asked Dec 13 '14 08:12

Ulrik


2 Answers

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).

like image 92
gniourf_gniourf Avatar answered Sep 24 '22 19:09

gniourf_gniourf


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
like image 33
perreal Avatar answered Sep 24 '22 19:09

perreal