zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: "Jesper Nygårds" <jesper.nygards@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Filtering array on index
Date: Thu, 25 Oct 2018 19:42:22 +0000	[thread overview]
Message-ID: <1540496542.2435852.1554725120.69463593@webmail.messagingengine.com> (raw)
In-Reply-To: <CAHYJk3RTBfnx0KgDO87VCZx=p+QUot_Q57F3F2N+6A7b38ZcHQ@mail.gmail.com>

Mikael Magnusson wrote on Thu, 25 Oct 2018 20:17 +0200:
> On 10/25/18, Jesper Nygårds <jesper.nygards@gmail.com> wrote:
> > I have an array which is the result of using zparseopts on a specification
> > that makes it possible to specify several filters with a -v flag. The
> > resulting array might look like the following:
> >
> > myarr=(-v filter1 -v filter2)
> >
> > I want to get rid of the "-v" elements, but only when they are the flag,
> > and not the argument. In other words, I would the like the array (-v
> > filter1 -v -v) to result in (filter1 -v).
> >
> > I had previously used:
> >
> > ${myarr:#-v}}
> >
> > which worked well, as I could accept the fact that -v was not possible to
> > use as the option argument.
> >
> > However, now I need to make sure that a -v argument is preserved, and I
> > thus need a compact way of specifying that I want to keep every other
> > element in the array.
> >
> > The most compact way I have found is:
> > for dash arg in $myarr; do myarr[(r)$dash]=()
> >
> > This works, but I was wondering if there's some more elegant solution,
> > preferrably without using iteration?
> 
> The above example doesn't work if you need to preserve the order (it
> will just remove the first $dash it finds).
> 
> % a=(-v filter1 -v -v -v middle -v -v -v final)
> % for dash arg in $a; do a[(r)$dash]=(); done; pl $a
> filter1
> middle
> -v
> -v
> final
> 
> middle "should" be between the two remaining -v.
> 
> This works, but is still a loop obviously:
> % for i in {$(($#a/2))..1}; do a[i*2-1]=(); done; pl $a
> filter1
> -v
> middle
> -v
> final
> 
> (Note that it is necessary to iterate backwards as the indices will
> change otherwise)

Another option:

myarr=(-v filter1 -v filter2)
() { local i j; myarr=(); for i j; do myarr+=($j) ; done } "${myarr[@]}"

In zparseopts context we can probably assume the arguments to the -v
option don't contain literal NUL bytes, so perhaps something like this:

% myarr=( -v 'foo foo' -v 'bar bar' -v '' )
% printf -v x '\0k1%s\0k2%s\0v' "$myarr[@]"
% x=${x//$'\0k1-v\0k2'}
% x=${x%$'\0v'}
% () { typeset -p argv } "${(@ps.\0v.)x}"
typeset -g -a argv=( 'foo foo' 'bar bar' '' )
% 

Is there a way to do this in O(N) time?  (The array-based solutions are
quadratic complexity due to indexing/appending being O(N).  I'm not sure
about the printf.)

Cheers,

Daniel

  parent reply	other threads:[~2018-10-25 19:42 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-25 11:29 Jesper Nygårds
2018-10-25 18:17 ` Mikael Magnusson
2018-10-25 18:55   ` Jesper Nygårds
2018-10-25 19:42   ` Daniel Shahaf [this message]
2018-10-25 20:21 ` Bart Schaefer
2018-10-25 20:42   ` dana
2018-10-26  4:14     ` Jesper Nygårds
2018-10-26  7:59       ` Jesper Nygårds
2018-10-26  8:21         ` dana
2018-10-26  9:28           ` Jesper Nygårds
2018-10-28  2:45           ` Bart Schaefer
2019-07-23  3:38 ` Sebastian Gniazdowski
2019-07-23  7:42   ` Sebastian Gniazdowski
2019-07-23  8:03     ` Sebastian Gniazdowski
2019-09-08  8:59     ` Sebastian Gniazdowski

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=1540496542.2435852.1554725120.69463593@webmail.messagingengine.com \
    --to=d.s@daniel.shahaf.name \
    --cc=jesper.nygards@gmail.com \
    --cc=zsh-users@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).