Ulrik
Ulrik

Reputation: 1141

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.

Upvotes: 6

Views: 3462

Answers (3)

John B
John B

Reputation: 3646

From the Bash Reference Guide:

The positional parameters are temporarily replaced when a shell function is executed (see Shell Functions).

So you could do this:

fullname()
{
    for first
    do
        echo "$first ${writer[$first]}"
    done
}
fullname "${!writer[@]}"

Upvotes: 0

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

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

Upvotes: 20

perreal
perreal

Reputation: 97958

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

Upvotes: 3

Related Questions