zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users <zsh-users@sunsite.dk>
Subject: Re: values of associations pointed by P flag
Date: Tue, 23 May 2006 08:14:00 -0700	[thread overview]
Message-ID: <060523081402.ZM15072@torch.brasslantern.com> (raw)
In-Reply-To: <20060522205404.GA24028@ulpmm.u-strasbg.fr>

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}
	}
    }

-- 


  reply	other threads:[~2006-05-23 15:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-22 20:54 Marc Chantreux
2006-05-23 15:14 ` Bart Schaefer [this message]
2006-05-25 14:43   ` Marc Chantreux
  -- strict thread matches above, loose matches on Subject: below --
2006-05-23 12:18 Marc Chantreux
2006-05-23 12:13 Marc Chantreux
2006-05-23 12:12 Marc Chantreux
2006-05-22 20:51 Marc Chantreux
2006-05-23 12:21 ` Marc Chantreux

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=060523081402.ZM15072@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).