Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: associative array questions




On 2022-12-11 11:53, Bart Schaefer wrote:
There are no pointers to data structures in zsh.
I know, I'm trying to fake it.
Parameter reference
is always by value.  Even ${(P)other} returns a value, it's just the
value of the parameter whose name is in turn the value of $other.
Anything you do with the result of ${(P)other} acts on that value, not
on the indirectly-named parameter itself.
So it seems.  I thought the (P) might force the issue.
There is one sort-of exception:  The ${NAME=WORD} and related
assign-and-substitute operations.  E.g., if you use
${(P)other::=text}, then the parameter named by $other is assigned the
value "text".  I say this is only sort-of an exception because if
$other names an array, you can only assign to the entire array, not to
individual subscripted fields.

If you want to assign indirectly to a particular field (whether a
position in an ordinary array or a named element in an associative)
you have to store the entire subscript expression in $other, such as
   other="aa[second]"
   : ${(P)other::='tea for two'}
That works.  I can intuit that parsing could be easier given the two stages, 'aa[second]' can only be interpreted one way.
Assignments are only parsed as such when there is an identifier
(string of nothing but alphanumerics or underscore, plus optional
subscript) to the left of the equal sign BEFORE any substitutions are
performed.
Before?  Nuts I thought the substitutions always happened first. Obviously not.  So leave expansion complexities on the right hand side.
  So this doesn't "look like" an assignment to the parser.

set -x reports it as looking like nothing.

Lacking the outer ${...} enclosing the subscript, ${(P)aarray} expands
to all the values of the pointed-to associative array, and you get the
equivalent of

   "1" "two" "three blind mice"[second]
Exactly.  I was trying to force it to accept it as an identifier.

You could also do (another thing that doesn't work in older zsh)
   eval "${aarray}+=([second]='tea for two')"
Of course I'm trying to avoid the eval entirely.  You've always advised against it.

Nope. It expands to a value, not a variable reference. Zsh doesn't
(yet) have what ksh calls "namerefs" ... block out everything you know
about pointers in C.
Well, at least I knew I was sinning.  Purely curiosity tho, I have no real issue to solve.  So we still have functionality to steal from ksh, that's interesting.
Associative arrays are unordered hash tables.  It might print "upside
down" or "inside out" depending on what keys are present.

Ok, so no order at all.  That's quite understandable, it would be extra overhead for the shell to even think about it and there's no need.

Thank you Sensei.






Messages sorted by: Reverse Date, Date, Thread, Author