Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: RFC PATCH: Sketch at :@ subscripting
- X-seq: zsh-workers 48272
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Mikael Magnusson <mikachu@xxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: RFC PATCH: Sketch at :@ subscripting
- Date: Sat, 27 Mar 2021 13:27:44 -0700
- Archived-at: <https://zsh.org/workers/48272>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2021-03/CAH%2Bw%3D7aRG1dfB5yAQ6yKV-GeHY7R_zWveHMU4%2B8QWNU9qSYkFA%40mail.gmail.com>
- In-reply-to: <20201219091314.dxslsscyiqffa3il@chazelas.org>
- List-id: <zsh-workers.zsh.org>
- References: <20201218131815.25999-1-mikachu@gmail.com> <20201219091314.dxslsscyiqffa3il@chazelas.org>
On Sat, Dec 19, 2020 at 1:13 AM Stephane Chazelas <stephane@xxxxxxxxxxxx> wrote:
>
> 2020-12-18 14:18:15 +0100, Mikael Magnusson:
> [...]
> > The idea is that you can do this:
> > % typeset -a somearray=( 'data here' 'some words' etc etc 1 2 3 4 ) idx=(1 3 5)
> > % echo ${somearray:@idx}
> > data here etc 1
> [...]
>
> Hi Mikael,
>
> I can't help but think that allowing to specify the indexes
> directly as perl does for instance in:
>
> print @list[1, 4, 2, 7..10, @idx, -1];
The way to do this (syntactically speaking) would be with a subscript
flag. E.g.:
print $array[(^)1,3,7]
would change the interpretation of the commas to select a set of
elements instead of a range. I chose (^) because of symmetry with
$^array and to avoid confusion with for example $array[@].
I haven't looked into how difficult that might be to implement,
particularly in assignment context.
Another possibility is to handle $array[{1,3,7}] specially since "{"
is already a syntax error in math context.
Both of those options could apply to associative arrays, although keys
would need to respect quoting to avoid troubles with an embedded ",".
> $ i=1,3
> $ echo ${a[i]}
> c
Here [i] is interpreted in math context so the comma becomes an
operator (the collision you already noted) so this becomes $a[3]
> $ echo ${a[$i]}
> a
This one is confusing and a bug ... it should be the same as
${a[$[$i]]} but instead it's ${a[,3]}. I haven't tracked down exactly
what's skipping everything up to but not including the first comma (it
is not, for example, just dropping one character).
Messages sorted by:
Reverse Date,
Date,
Thread,
Author