zsh-users
 help / color / mirror / code / Atom feed
* values of associations pointed by P flag
@ 2006-05-23 12:18 Marc Chantreux
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Chantreux @ 2006-05-23 12:18 UTC (permalink / raw)
  To: zsh-users

Hi all,

I would like to write a function that compare 2 associative arrays. I
can use eval but the P flag looks better. So i wrote some tests :


# expected is

= 12
= 24


typeset -A u; u=( a 12 b 24 ) x=u

for k ( ${(Pk)x} ) print $k'= '${(P)x[$k]}
a= 12 24
b= 12 24

for k ( ${(Pk)x} ) print $k'= '${${(P)x}[$k]}
a= 12
b= 12

... tried other unsuccessfull syntaxes.

Do i have a chance to don't use eval ?


regards
mc


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

* Re: values of associations pointed by P flag
  2006-05-23 15:14 ` Bart Schaefer
@ 2006-05-25 14:43   ` Marc Chantreux
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Chantreux @ 2006-05-25 14:43 UTC (permalink / raw)
  To: zsh-users

le 23/05/2006,
Bart Schaefer nous écrivait :
> } I would like to write a function that compare 2 associative arrays.
> 
> I take it this means you'd like to write a function that takes the
> names of 2 associative arrays as arguments and compares the contents
> of those associations.

yes .. that's the fact.

> } I can use eval but the P flag looks better.
> 
> It might, but you misunderstand how it works.  (And the doc could be
> clearer, but it all stems from the fact that zsh parameter expansion
> works by passing values of parameters, not references to parameters.)

enlightenment ! thanks for your detailed explanation and exemple : my
code works fine now ! 

regards,
mc


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

* Re: values of associations pointed by P flag
  2006-05-22 20:54 Marc Chantreux
@ 2006-05-23 15:14 ` Bart Schaefer
  2006-05-25 14:43   ` Marc Chantreux
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2006-05-23 15:14 UTC (permalink / raw)
  To: zsh-users

On May 22, 10:54pm, Marc Chantreux wrote:
}
} I would like to write a function that compare 2 associative arrays.

I take it this means you'd like to write a function that takes the
names of 2 associative arrays as arguments and compares the contents
of those associations.

} I can use eval but the P flag looks better.

It might, but you misunderstand how it works.  (And the doc could be
clearer, but it all stems from the fact that zsh parameter expansion
works by passing values of parameters, not references to parameters.)

When you write
    hash=( a 1 b 2 )
    x=hash
    print ${${(P)x}[b]}
that's equivalent to writing
    print ${${hash}[b]}
but the value of ${hash} is an ordinary array consisting of all the
values in the associative array.  Consequently you can't subscript
that using the associative array keys.  When instead you write
    print ${(P)x[key]}
that's equivalent to
    print ${(P)${x[key]}}
which in the example above is equivalent to
    print ${h}
because $x is a string and [key] is interpreted as a math expression
with value zero, and the zero'th substring of a string is its first
letter.  Your test happened to partly work only because you used all
variables with single-letter names.

The right answer is that you have to put the subscript into the value
of $x before you apply the (P) flag.
    x="hash[b]"
    print ${(P)x}

So the completed function looks something like

    cmp_hashes() {
	local __k __x __y   # Underscores to avoid name clash
	for __k ( ${(Pk)1} ) {
	    __x="$1""[$__k]" __y="$2""[$__k]"
	    print $__x=${(P)__x} $__y=${(P)__y}
	}
    }

-- 


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

* Re: values of associations pointed by P flag
  2006-05-22 20:51 Marc Chantreux
@ 2006-05-23 12:21 ` Marc Chantreux
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Chantreux @ 2006-05-23 12:21 UTC (permalink / raw)
  To: zsh-users

Hi all,

I apologize for the noise : i had a problem with my mailchain and 
thought that this question was never posted.

Please excuse me all, i'm very sorry.

regards
mc


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

* values of associations pointed by P flag
@ 2006-05-23 12:13 Marc Chantreux
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Chantreux @ 2006-05-23 12:13 UTC (permalink / raw)
  To: zsh-users

Hi all,

I would like to write a function that compare 2 associative arrays. I
can use eval but the P flag looks better. So i wrote some tests :


# expected is 

= 12
= 24


typeset -A u; u=( a 12 b 24 ) x=u

for k ( ${(Pk)x} ) print $k'= '${(P)x[$k]}
a= 12 24
b= 12 24

for k ( ${(Pk)x} ) print $k'= '${${(P)x}[$k]}
a= 12
b= 12

... tried other unsuccessfull syntaxes.

Do i have a chance to don't use eval ? 


regards
mc

-- 
tel: 03 90 24 50 00
mel: marc.chantreux@ulpmm.u-strasbg.fr
---------------------------------------


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

* values of associations pointed by P flag
@ 2006-05-23 12:12 Marc Chantreux
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Chantreux @ 2006-05-23 12:12 UTC (permalink / raw)
  To: zsh-users

Hi all,

I would like to write a function that compare 2 associative arrays. I
can use eval but the P flag looks better. So i wrote some tests :


# expected is 

= 12
= 24


typeset -A u; u=( a 12 b 24 ) x=u

for k ( ${(Pk)x} ) print $k'= '${(P)x[$k]}
a= 12 24
b= 12 24

for k ( ${(Pk)x} ) print $k'= '${${(P)x}[$k]}
a= 12
b= 12

... tried other unsuccessfull syntaxes.

Do i have a chance to don't use eval ? 


regards
mc

-- 
tel: 03 90 24 50 00
mel: marc.chantreux@ulpmm.u-strasbg.fr
---------------------------------------


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

* values of associations pointed by P flag
@ 2006-05-22 20:54 Marc Chantreux
  2006-05-23 15:14 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Chantreux @ 2006-05-22 20:54 UTC (permalink / raw)
  To: zsh-users

Hi all,

I would like to write a function that compare 2 associative arrays. I
can use eval but the P flag looks better. So i wrote some tests :


# expected is 

= 12
= 24


typeset -A u; u=( a 12 b 24 ) x=u

for k ( ${(Pk)x} ) print $k'= '${(P)x[$k]}
a= 12 24
b= 12 24

for k ( ${(Pk)x} ) print $k'= '${${(P)x}[$k]}
a= 12
b= 12

... tried other unsuccessfull syntaxes.

Do i have a chance to don't use eval ? 


regards
mc

-- 
tel: 03 90 24 50 00
mel: marc.chantreux@ulpmm.u-strasbg.fr
---------------------------------------


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

* values of associations pointed by P flag
@ 2006-05-22 20:51 Marc Chantreux
  2006-05-23 12:21 ` Marc Chantreux
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Chantreux @ 2006-05-22 20:51 UTC (permalink / raw)
  To: zsh-users

Hi all,

I would like to write a function that compare 2 associative arrays. I
can use eval but the P flag looks better. So i wrote some tests :


# expected is 

= 12
= 24


typeset -A u; u=( a 12 b 24 ) x=u

for k ( ${(Pk)x} ) print $k'= '${(P)x[$k]}
a= 12 24
b= 12 24

for k ( ${(Pk)x} ) print $k'= '${${(P)x}[$k]}
a= 12
b= 12

... tried other unsuccessfull syntaxes.

Do i have a chance to don't use eval ? 


regards
mc

-- 
tel: 03 90 24 50 00
mel: marc.chantreux@ulpmm.u-strasbg.fr
---------------------------------------


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

end of thread, other threads:[~2006-05-25 14:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-23 12:18 values of associations pointed by P flag Marc Chantreux
  -- strict thread matches above, loose matches on Subject: below --
2006-05-23 12:13 Marc Chantreux
2006-05-23 12:12 Marc Chantreux
2006-05-22 20:54 Marc Chantreux
2006-05-23 15:14 ` Bart Schaefer
2006-05-25 14:43   ` Marc Chantreux
2006-05-22 20:51 Marc Chantreux
2006-05-23 12:21 ` Marc Chantreux

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).