Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Implement an "array index" subscript flag
- X-seq: zsh-workers 42950
- From: dana <dana@xxxxxxx>
- To: "zsh-workers@xxxxxxx" <zsh-workers@xxxxxxx>
- Subject: Re: Implement an "array index" subscript flag
- Date: Fri, 8 Jun 2018 00:16:20 -0500
- Cc: Michael Milton <michael.milton@xxxxxxxxxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=cjH2RGYEEqMinGogPk51BnlywegGW4swekV7qeRvXIU=; b=1hoU0tBnVCI3LPYMTKOX4UxPW+2s0D7b3CjSdjx4DifDrqE3LVQo2J9s73cIWLhRxv k28Empd4leV76QSegjad/IDCaVaU4pnShGOWShwAVw71cX9cNB9DlHOz/PEcB43du1cm H9cfsCUFN16Zin1ym5YBftFuDVDnlSr5VaqjHDI5rrNI+YBHAuXPEqByQyevLsRsueYj kiOEwcrOSa3kAlNVG79mWGUPm1ZCb6idv+SNczObH3IaF30BJGKbE4vJLmPsAh9D19bV A7zQl7b/wvAiv6lrmrku2N51vtlUX67RX5q+XOJERkIedY9sEoNy8H94SOHDK3EaaBH8 PkiA==
- In-reply-to: <SYXPR01MB1069D97AEB4D3F17C1E1E43DBA7B0@SYXPR01MB1069.ausprd01.prod.outlook.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <SYXPR01MB1069D97AEB4D3F17C1E1E43DBA7B0@SYXPR01MB1069.ausprd01.prod.outlook.com>
On 7 Jun 2018, at 20:37, Michael Milton <michael.milton@xxxxxxxxxxxxxx> wrote:
>A useful feature for zsh would be the ability to convert an array into an array
>of keys/indexes for that array.... Bash has a syntax for this, with the
>${!array[@]} expansion
zsh actually has the (k) flag for this, but it only works on associations. zsh
also supports the ! syntax (which bash 3 borrowed from ksh93) when in ksh-
emulation mode, but since it's effectively an alias for (k) it *also* works only
on associations. To be clear, in both bash 4 and ksh93 it works as expected on
both.
I feel like the intuitive thing would be for (k) and (v) to work on arrays
equivalently to the way they work on associations. In other words...
% arr=( foo bar baz )
% print ${(k)arr}
1 2 3
% print ${(kv)arr}
1 foo 2 bar 3 baz
Looking at the manual, it seems to me that the current effect of those two flags
on arrays is actually undefined (except when used in conjunction with a
subscript), so... would that not be a reasonable change to make, if someone were
inclined?
Looks like Oliver was considering exactly that, once upon a time:
http://www.zsh.org/mla/workers/2004/msg00226.html
In the mean time, though, like the Stack Exchange link says — since zsh doesn't
support sparse arrays, i think {1..$#arr} is almost exactly equivalent to (the
hypothetical) ${(k)arr}.
dana
Messages sorted by:
Reverse Date,
Date,
Thread,
Author