zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Cc: Michael Milton <michael.milton@unimelb.edu.au>
Subject: Re: Implement an "array index" subscript flag
Date: Fri, 8 Jun 2018 00:16:20 -0500	[thread overview]
Message-ID: <075A01C0-8DA7-4D1B-8553-1C1C2B66D0F2@dana.is> (raw)
In-Reply-To: <SYXPR01MB1069D97AEB4D3F17C1E1E43DBA7B0@SYXPR01MB1069.ausprd01.prod.outlook.com>

On 7 Jun 2018, at 20:37, Michael Milton <michael.milton@unimelb.edu.au> 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


  reply	other threads:[~2018-06-08  5:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-08  1:37 Michael Milton
2018-06-08  5:16 ` dana [this message]
2018-06-08  5:46   ` Michael Milton
2018-06-08  6:15     ` dana
2018-06-08  6:27       ` Michael Milton
2018-06-08  8:25   ` Peter Stephenson

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=075A01C0-8DA7-4D1B-8553-1C1C2B66D0F2@dana.is \
    --to=dana@dana.is \
    --cc=michael.milton@unimelb.edu.au \
    --cc=zsh-workers@zsh.org \
    /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).