2

Is there a way in Bash to make a pointer to the value of a key in an associate array? Like this:

declare -A mapp
mapp=( ["key"]="${value}" )

for k in "${!mapp[@]}"; do 
    pointer="${mapp["${k}"]}"   # How do I do this?
done

Usually, you do not need to use a pointer, but I'm curious to see if there's a way to make one.

In a simpler situation (i.e., for normal/string variables), I would make a pointer like this:

pointer=b
read -p "Enter something: " b
eval pointer=\$${pointer}

How would I do this for an associate array? This doesn't work (skip the strikethroughed code):

declare -A mapp
mapp=( ["first"]="${a}"  ["second"]="${b}" )

for k in "${!mapp[@]}"; do 
    v=mapp["${k}"]

    read -p "Enter ${k}: " new

    eval v=\$${v}    # Doesn't work

done

declare -A mapp
mapp=( ["first"]="${a}"  ["second"]="${b}" )

for k in "${!mapp[@]}"; do 
    v=mapp["${k}"]

    read -p "Enter ${k}: " k

    eval v=\$${v}    # Doesn't work

done

This doesn't work either (skip the strikethroughed code):

declare -A mapp
mapp=( ["first"]="${a}"  ["second"]="${b}" )

for k in "${!mapp[@]}"; do 
    v=mapp

    read -p "Enter ${k}: " new

    eval v=\$${v["${k}"]}    # Doesn't work (and has terrible readability)

done

declare -A mapp
mapp=( ["first"]="${a}"  ["second"]="${b}" )

for k in "${!mapp[@]}"; do 
    v=mapp

    read -p "Enter ${k}: " k

    eval v=\$${v["${k}"]}    # Doesn't work (and has terrible readability)

done
5
  • So you don't mean "pointer" here at all really? You mean how do you indirectly/etc. get the value from array? Commented Apr 16, 2015 at 18:54
  • Sorry, I just realized I messed up something in my code. I'm trying to use the pointer to change the value in the array. Commented Apr 16, 2015 at 18:57
  • What about pointer="${mapp["${k}"]}" didn't work to get pointer to have the value you wanted? (Other than the inner quotes being unnecessary and wrong.) Or do you really mean a "pointer" (of sorts) so you can then use $pointer to get the current value from the associative array? Commented Apr 16, 2015 at 18:58
  • None of your examples are about assigning through the "pointer". Perhaps you want to fix that? Commented Apr 16, 2015 at 19:00
  • mywiki.wooledge.org/BashFAQ/006 Commented Apr 16, 2015 at 19:03

2 Answers 2

8

In bash 4.3, you can use a nameref:

$ mapp=([key]=value)
$ declare -n x=mapp[key]  # NO dollar sign!
$ x=7
$ echo ${mapp[key]}
7

Before 4.3, you need to use the declare command differently to do the indirection.

$ mapp=([key]=value)
$ x=mapp[key]  # NO dollar sign!
$ declare "$x=7"
$ echo ${mapp[key]}
7
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks! I never thought to check the options for declare.
1

No problem:

$ declare -A ary=([foo]=bar [baz]=qux)
$ key=foo
$ pointer="ary[$key]"
$ echo "$pointer"
ary[foo]
$ echo "${!pointer}"
bar

A "pointer" in this sense is an indirect variable

3 Comments

Ah, I missed the point about using the "pointer" to set a value.
That's ok. So did the OP for a while. =)
Touché Etan Reisner :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.