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
pointer="${mapp["${k}"]}"didn't work to getpointerto 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$pointerto get the current value from the associative array?