From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: zsh-users-return-23719-ml=inbox.vuxu.org@zsh.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 2ed3342f for ; Thu, 25 Oct 2018 18:17:41 +0000 (UTC) Received: (qmail 26467 invoked by alias); 25 Oct 2018 18:17:22 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23719 Received: (qmail 8655 invoked by uid 1010); 25 Oct 2018 18:17:21 -0000 X-Qmail-Scanner-Diagnostics: from mail-yw1-f42.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(209.85.161.42):SA:0(-1.9/5.0):. Processed in 1.68888 secs); 25 Oct 2018 18:17:21 -0000 X-Envelope-From: mikachu@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | 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== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=/JCgPAM9EGWL9MXvZ9PN2pZ/27pBJhJhaWPmHiJWScc=; b=n3W2L39SjbZVnxkTuAcbryQMTaq4AmGU05cHczm0sR6Fh/twjvUlKy/ijuB4fO6Pez 7HcGnsXOlisxYxhe+IoBKzoF8B+S5olLLlcUEIUjksCJQfFCUul0cI1BhQkUs8OT01zu 3YL0eRjlUF5RfDVDDM90ik94NkIcM/CDwaeapnQA6dl0BJgU+7zMNThIItq8HtA2gCzl l01mDiXVIqncg5jjVWHkb+UUa9/aRT3AZmmmr8zD3QfH2ED3+8Kk/BqBSQCG1BvBds/b nd14ZuCpI+1Fp6uG+NTAqXLE9fV1UrTpQ9ZsD9KEgcM5NWPgDSNA0sz3hmY58qi9nrKV f5vQ== X-Gm-Message-State: AGRZ1gIl2PRSztajUQpcUGjcm9YVPX+L3FC3WZ3WPBFT2VYshAal3p/n lueR5c4Je5BE9Pc1riDfVxi0u7WlE17tRGDeiwkzjA== X-Google-Smtp-Source: AJdET5dnRpED8N0pHV5py9klbJ8TqxjghCtHZjTn6mo7J5Hf2Jd3vBGaW/NxmDmvRydMEeJURxRlTH2nNqvXgfashac= X-Received: by 2002:a0d:f585:: with SMTP id e127-v6mr373711ywf.170.1540491436559; Thu, 25 Oct 2018 11:17:16 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Mikael Magnusson Date: Thu, 25 Oct 2018 20:17:15 +0200 Message-ID: Subject: Re: Filtering array on index To: =?UTF-8?Q?Jesper_Nyg=C3=A5rds?= Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On 10/25/18, Jesper Nyg=C3=A5rds wrote: > I have an array which is the result of using zparseopts on a specificatio= n > that makes it possible to specify several filters with a -v flag. The > resulting array might look like the following: > > myarr=3D(-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]=3D() > > 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=3D(-v filter1 -v -v -v middle -v -v -v final) % for dash arg in $a; do a[(r)$dash]=3D(); 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]=3D(); 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=3D( ${${:--v}:^^a} ); pl $a -v filter1 -v -v -v middle -v -v -v final --=20 Mikael Magnusson