Bash indirect reference to an associative array

后端 未结 3 1239
醉酒成梦
醉酒成梦 2020-12-18 07:31

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
writ         


        
相关标签:
3条回答
  • 2020-12-18 08:18

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

    0 讨论(0)
  • 2020-12-18 08:22
    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
    
    0 讨论(0)
  • 2020-12-18 08:22

    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[@]}"
    
    0 讨论(0)
提交回复
热议问题