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

Re: Filtering array on index



On Tue, 23 Jul 2019 at 05:38, Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> wrote:
>
> On Thu, 25 Oct 2018 at 13:30, Jesper Nygårds <jesper.nygards@xxxxxxxxx> wrote:
> > myarr=(-v filter1 -v filter2)
>
> I've recently had this problem. One other way to do it:
>
> ~ idx=0; olen=$#a+1; a=( ${a[@]/(#m)*/$a[$(( ++idx % 2 ? olen : idx ))]} )

Turns out the expression can be simpler because the math mode within
array's index is implicit, so:

  idx=0; olen=$#a+1; a=( ${a[@]/(#m)*/$a[ ++idx % 2 ? olen : idx ]} )

I've also tried to construct an ::= assignment version. A safe.
different output variable version is:
  b=(); idx=0; olen=$#a+1; : ${a[@]/(#m)*/${b[++idx]::=${a[idx % 2 ?
olen : idx]}}}

However, this yields 8 empty elements (for the a=(-v filter1 -v filter2 ))::

% print -rl -- "${(q)b[@]}"
''
''
...

prepending $idx after ::= yields following output:
''
1
''
3
''
5
''
7

I wonder what's going on? The code looks OK. Maybe it's a bug?

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org



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