Robert Neville wrote: > On 9/20/07, David Korn <dgk at research.att.com> wrote: > > cc: rbtneville at gmail.com > > Subject: Re: [ksh93-integration-discuss] Dereferencing an array name > > reference > > > While educating myself about the new ksh features I stumbled over a > > > behaviour which I do not understand. The example below tries to > > > dereference a name reference but only returns the name of the variable > > > but not the array index: > > > % ksh93 > > > $ typeset -A a > > > $ a[x]=5 > > > $ nameref l=a[x] > > > echo "${!l}"' > > > a > > > > > > I expected a[x] as output. Is this a bug or do name references only > > > apply to variables and not their index? > > > -- > > > robert neville - it consultant > > > > I will classify this as a bug and it also uncovers another bug > > or at least undocumented behavior. > > How do nameref variables work internally? I had the impression that a > nameref variable only remembers the name of the variable and places > this string in place of the nameref name during execution.
"nameref" variables are a bit more than that... AFAIK one of the features of "nameref"'s is that they can "look" into the scope of the calling function's (e.g. the "parent scope") and can therefore access local function variables of the caller (assuming you have the name of the variable). For example... -- snip -- function y { integer foo=19 x foo } function x { nameref g=$1 print "g=${g}" } y -- snip -- ... will print... -- snip -- $ ksh93 nameref001.ksh g=19 -- snip -- ... e.g. the local variable "foo" of function "y" is visible in the function "x" via nameref. > > Currently, > > ${!x} > > should provide the name for variable x following references, so > > it should have displayed a[x] in this case. (I will fix this > > and also will not allow the [..] to be treated as a character class > > even if not quoted. > > > > Currently ${...@]} or ${!x[*]} expand to the subscripts (or 0 if x is > > not subscripted). > > > > However, the behavior of > > ${!x[foo]} > > is currently undocumented and currently outputs foo. This will be changed > > Why do you think that this is undocumented? I'd expect that if x is an > element in an array [foo] describes the 2nd dimension, expanding to > a[x][foo]. Or am I wrong? AFAIK ksh93 does not support multidimensional variables that way. If you want more than one dimension it's better to do it via associative arrays (which take strings as index, e.g. in your example a["${x}_${foo}] would be used instead). ---- Bye, Roland -- __ . . __ (o.\ \/ /.o) roland.mainz at nrubsig.org \__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer /O /==\ O\ TEL +49 641 7950090 (;O/ \/ \O;)