From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29385 invoked from network); 23 May 2006 15:14:27 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.1 (2006-03-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00, FORGED_RCVD_HELO autolearn=ham version=3.1.1 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 23 May 2006 15:14:27 -0000 Received: (qmail 12099 invoked from network); 23 May 2006 15:14:20 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 23 May 2006 15:14:20 -0000 Received: (qmail 19870 invoked by alias); 23 May 2006 15:14:13 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 10297 Received: (qmail 19861 invoked from network); 23 May 2006 15:14:13 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 23 May 2006 15:14:13 -0000 Received: (qmail 10989 invoked from network); 23 May 2006 15:14:13 -0000 Received: from vms046pub.verizon.net (206.46.252.46) by a.mx.sunsite.dk with SMTP; 23 May 2006 15:14:12 -0000 Received: from torch.brasslantern.com ([71.116.105.50]) by vms046.mailsrvcs.net (Sun Java System Messaging Server 6.2-4.02 (built Sep 9 2005)) with ESMTPA id <0IZQ005O44ZFSBC1@vms046.mailsrvcs.net> for zsh-users@sunsite.dk; Tue, 23 May 2006 10:14:04 -0500 (CDT) Received: from torch.brasslantern.com (localhost.localdomain [127.0.0.1]) by torch.brasslantern.com (8.13.1/8.13.1) with ESMTP id k4NFE2Fx015074 for ; Tue, 23 May 2006 08:14:02 -0700 Received: (from schaefer@localhost) by torch.brasslantern.com (8.13.1/8.13.1/Submit) id k4NFE2eX015073 for zsh-users@sunsite.dk; Tue, 23 May 2006 08:14:02 -0700 Date: Tue, 23 May 2006 08:14:00 -0700 From: Bart Schaefer Subject: Re: values of associations pointed by P flag In-reply-to: <20060522205404.GA24028@ulpmm.u-strasbg.fr> To: zsh-users Message-id: <060523081402.ZM15072@torch.brasslantern.com> MIME-version: 1.0 X-Mailer: OpenZMail Classic (0.9.2 24April2005) Content-type: text/plain; charset=us-ascii References: <20060522205404.GA24028@ulpmm.u-strasbg.fr> Comments: In reply to Marc Chantreux "values of associations pointed by P flag" (May 22, 10:54pm) 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} } } --