Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: easy calling of associative array?
On Nov 2, 7:43am, Ray Andrews wrote:
}
} get_v ()
} {
} #$3=${(P)${:-${1}[$2]}} #nothing works
} eval "$3=\$${1}[$2]" #this works
} }
This has nothing to do with associative arrays and everything to do with
assignment syntax. "$3" is not a valid identifier, so "$3=" is not an
assignment; this all decided before $3 is expanded, which is why putting
the "eval" around it works (it delays the "is an identifier" check until
after $3 has expanded).
Probably the most cogent way to do this is
get_v () {
typeset -g $3="${(P)${:-${1}[$2]}}"
}
} set_v ()
} {
} # {(P)${:-${1}[$2]}}=$3 #nothing works
} eval "${1}[$2]=$3" #this works
} }
Same assignment-syntax problem.
set_v () {
# typeset -gA $1 # optional
typeset -g "${1}[$2]=$3" # quotes so [ ] isn't globbing
}
Here you don't need the (P) indirection because ${1} and $2 are both
expanded before being passed to typeset, so you already extracted the
name that was passed in $1.
Also note I'm ignoring all possible error checking, e.g. if $1 is not
an identifier (in the worst case, contains an "="), things go badly.
} test_v ()
} {
} #eval "${1}[$2]" #nothing works
} #this works
} [ ${(P)${:-${1}[$2]}} = $3 ] && echo 'all too true' || echo 'alas, no.'
} }
I'm not exactly sure what you're wanting as either output or exit
status here, but except that I'd recommend [[ ]] instead of [ ] as
the test syntax, what you wrote for "this works" is sensible.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author