Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Filtering array on index
- X-seq: zsh-users 23719
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Jesper Nygårds <jesper.nygards@xxxxxxxxx>
- Subject: Re: Filtering array on index
- Date: Thu, 25 Oct 2018 20:17:15 +0200
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=/JCgPAM9EGWL9MXvZ9PN2pZ/27pBJhJhaWPmHiJWScc=; b=BhzJnlCA0x473HPynjDz7ycKjgmOFx0HSnkIKfG7SYjC0D826SYRKFYpZbc6e7mdAc cEbjNgBMI8zDfuQ0JDPQcMkmq/pCuTl0SQegYE5LvY54BGUPUS0bedvxlkCnnBee17gM lWkbXjRaikRADMZzawTE9N9r0NQvPyUDdJqhZhNb2CQOg2fQlaezsVvwhR9tlWllvyEn aElUReY2GdmLULpaYjpCaYFaZlgddS6Fxpchq+oah6wQPa76xAj/13+xuayxldJ8KQ0D lsa2M4fZumWH4INb7Aw8y94bdbkBAyDx54JXHJsS5b55CnkYgc4DWl9PGFYRQ3uDpAta FjDw==
- In-reply-to: <CABZhJg80vQuMHGKABkzR1bdtt89Akpkfd8kWOV83N-qvqioaXw@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CABZhJg80vQuMHGKABkzR1bdtt89Akpkfd8kWOV83N-qvqioaXw@mail.gmail.com>
On 10/25/18, Jesper Nygårds <jesper.nygards@xxxxxxxxx> 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)
It is very easy to add -v back again however ;)
% a=( ${${:--v}:^^a} ); pl $a
-v
filter1
-v
-v
-v
middle
-v
-v
-v
final
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author