From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27774 invoked by alias); 10 Sep 2017 02:09:37 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 22879 Received: (qmail 10524 invoked by uid 1010); 10 Sep 2017 02:09:37 -0000 X-Qmail-Scanner-Diagnostics: from mx.spodhuis.org by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(94.142.241.89):SA:0(-4.2/5.0):. Processed in 2.413112 secs); 10 Sep 2017 02:09:37 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: zsh-workers+phil.pennock@spodhuis.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=spodhuis.org; s=d201708; h=In-Reply-To:Content-Type:MIME-Version:References :Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding :Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=cV0ZM2fAwk3zF222zgvsUo2TnADmL1uU6TlhX7sQtRU=; b=ivO3G3wIiKaVXmz6eRJRc3ugmH odCr5/SiIK6dDbqjjn/ew01GK0+vJRkT+wFsmSI2RUKkQtrTTym1WIscI+UU/uHvMHdZWeq070/yM u2Wt+3yNQ3vMLySbS9KoIogMVhGle6zsCIYHZB/2S6Xpkq7bOPtcYXBosCBKHyFGAUVhPwzaHno+W LMJHs1SMs8O3gOz6ZACsRljPeibU; Date: Sat, 9 Sep 2017 21:51:41 -0400 From: Phil Pennock To: jdh Cc: zsh-users@zsh.org Subject: Re: ksX-Mailer: MH-E 8.6; GNU Mailutils 3.2; GNU Emacs 24.5.1 Message-ID: <20170910015141.GA12309@tower.spodhuis.org> References: <20170909170037.5276@sparky> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170909170037.5276@sparky> OpenPGP: url=https://www.security.spodhuis.org/PGP/keys/0x4D1E900E14C1CC04.asc On 2017-09-09 at 17:00 +1700, jdh wrote: > I wanted to use the array flage (i) to get an index into a charagcter array (a string), but am getting incorrect results. Here is an example code snippet which shows the oddity. > > # > # below define a table (string) with 4 characters. > chrtab="#()*" > for (( ndx=1; ndx<=$#chrtab ; ndx++ )) > do echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(i)$chrtab[ndx]]" > done > > I expect the result to be 1, 2, 3, 4 > but get 5, 5, 5, 1 > > Is this a a feature or a bug? Both, I think? The `i` is documented to return one past the length of the item, so `5` means "not found". You can use `I` and see `0` instead. It's because those are pattern characters, thus the `*` matching the first item and returning `1`. So I'd _expect_ that using the `e` flag too would resolve it, but it only fixes two of those. for (( ndx=1; ndx<=$#chrtab ; ndx++ )); do echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(ie)$chrtab[ndx]]" done Result: 5, 5, 3, 4 What works instead is to backslash-escape the metacharacters: for (( ndx=1; ndx<=$#chrtab ; ndx++ )); do echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(i)${(q)chrtab[ndx]}]" done Result: 1, 2, 3, 4 I'm not off-hand seeing why `(ie)` indexing wouldn't handle the `#` and `(` entries though, which is why I think perhaps a bug. But I'm probably wrong and I too would be interested in learning why this is expected behavior. :) -Phil