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

Re: RFC PATCH: Sketch at :@ subscripting



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];

And in assignments as well:

@list[@idx] = qw(new values here);

would be more useful.

It's unfortunate that "," is used for ranges in $a[1,3] in zsh
especially considering that "," is also an arithmetic operator
(btw:
  $ a=({a..z})
  $ echo ${a[1,3]}
  a b c
  $ i=1,3
  $ echo ${a[i]}
  c
  $ echo ${a[$i]}
  a
??) and that {1,3,{5..7}} otherwise follows the perl semantics
(and extends it as it allows backward ranges).

${list[2 6 8]} in zsh wouldn't work as ${list[2 -1]} already
means something different, but maybe ${list[2;6,9;${(j[;])idx},-1]}
would?

That would mean no backward range as $a[3,1] ATM is the empty
list but backward sets would still be possible with $a[3;2;1].

Maybe another option could be $a[{1,4,{8..2},$^idx}] so as to
reuse an already known syntax?

Except that:

$ i=(a b)
$ echo {1,2,$^i}.
1. 2. a. 1. 2. b.
$ echo {1,2,{a,b}}.
1. 2. a. b.

Syntax would also have to be restricted to a[{...}], no
a[{1,2}0] for instance.

a[{${(f)^"$(shuf -n 5 -i 1-100)"}]=({a..e})
soon becomes awkward as well.

There's also the question of

a[1;5]=(a b c)
a[1;5]=(a)
...

Having a syntax that could also apply to associative arrays
would be best.

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author