Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: RFC PATCH: Sketch at :@ subscripting



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