zsh-workers
 help / color / mirror / code / Atom feed
* Bug with associative array and P flag?
@ 2004-03-30  2:14 Jonathan Hankins
  2004-03-30 17:21 ` Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Hankins @ 2004-03-30  2:14 UTC (permalink / raw)
  To: zsh-workers

Hi,

Below are two examples of using the P flag to variable expansion to access an 
array indirectly.  When using a regular array (foo), this works correctly.  
However, when using an associative array in the same way, I get the value of 
the first (alphabetically? randomly?) key (animal) regardless of the key I 
use.  Should the P flag work with the associative array in the same way as it 
does with the regular array?

I have tested with 4.2.0, 4.1.1 and 4.0.9 on Debian GNU/Linux.

% typeset -a foo
% typeset -A bar
%
% foo=(this is an array)
%
% bar[color]=red
% bar[animal]=cat
% bar[fruit]=apple
%
%
% ref=foo
% print ${${(P)ref}[1]}
this
% print ${${(P)ref}[2]}
is
% print ${${(P)ref}[3]}
an
% print ${${(P)ref}[4]}
array
%
% ref=bar
% print ${${(P)ref}[color]}
cat
% print ${${(P)ref}[animal]}
cat
% print ${${(P)ref}[fruit]}
cat
%

Just for kicks, this works as expected:

% print ${${(P)ref}}
cat apple red
% print ${${(Pk)ref}}                                               
animal fruit color
% print ${${(Pkv)ref}}
animal cat fruit apple color red

Thanks,

-Jonathan Hankins


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Bug with associative array and P flag?
  2004-03-30  2:14 Bug with associative array and P flag? Jonathan Hankins
@ 2004-03-30 17:21 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2004-03-30 17:21 UTC (permalink / raw)
  To: Jonathan Hankins; +Cc: zsh-workers

Jonathan Hankins wrote:

> Below are two examples of using the P flag to variable expansion to access an 
> array indirectly.  When using a regular array (foo), this works correctly.  
> However, when using an associative array in the same way, I get the value of 
> the first (alphabetically? randomly?) key (animal) regardless of the key I 
> use.  Should the P flag work with the associative array in the same way as it 
> does with the regular array?

This isn't so much a bug as a design limitation. It has nothing in
particular to do with the P flag. Note that:
  print ${${bar}[color]}
would print `cat' just like ${${(P)ref}[color]}

Note also:
  % print $bar
  cat apple red

What is happening is that the inner expansion is returning this as an
array of three items. Using [color] as an index is then equivalent to
using [0] because it is taken as a regular array index and hence a math
expression. Note the difference if you assign color=2 first. Ideally,
we would need an explicit (v) for it to work this way.

For this to work, I think we will need the long overdue major changes
to the parameter code. That's because I don't think the nested
expansion code handles anything other than scalars and arrays for the
nested parameters.

Oliver


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-03-30 17:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-30  2:14 Bug with associative array and P flag? Jonathan Hankins
2004-03-30 17:21 ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).