From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8906 invoked by alias); 15 Sep 2015 06:15:24 -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: X-Seq: 20564 Received: (qmail 18792 invoked from network); 15 Sep 2015 06:15:23 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, HTML_MESSAGE,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=+8xMcyrNOeoI3TQhJLJlAosx/JXzHLI1B1YeWS+u73A=; b=em7TDTrNOQswf8Djo1IPvO95VcJkc+UBiehJ6vX2/ppAedcx7RenuaPN8Tmj1SxPHC OujjDWpUjQdQ/tKMEbU7WdEW4XZSNrvYdZJ3ZZagCmC+hpiGhECKb60kWOQIyUf4j8Ot KcM0t3HqUQYCmDO+duqXUdLnru/p3VQtYy06qntXwy/OjZjux0tAPY0y4+a8zCc/Fqcv 6OxcRo9lbQ3505xNCEFNhTn/g0sf27ljJl2dYhfXgRnSnPmCdXvCNcR8+IfHmJksKXM+ 8lAuOuPc8z7J7UwQN3hknjgZpGpj2W/jOGy21dNiZRDiFXVtf2KZcKpNsYv8qUK46vjV J5Hg== MIME-Version: 1.0 X-Received: by 10.152.21.9 with SMTP id r9mr11951739lae.1.1442297719382; Mon, 14 Sep 2015 23:15:19 -0700 (PDT) In-Reply-To: <150914144654.ZM26119@torch.brasslantern.com> References: <150914144654.ZM26119@torch.brasslantern.com> Date: Tue, 15 Sep 2015 08:15:19 +0200 Message-ID: Subject: Re: Splitting into a one element array From: =?UTF-8?Q?Jesper_Nyg=C3=A5rds?= To: Zsh Users Content-Type: multipart/alternative; boundary=089e013d2024396a16051fc31bec --089e013d2024396a16051fc31bec Content-Type: text/plain; charset=UTF-8 Ah yes, now I see. Thank you both! On Mon, Sep 14, 2015 at 11:46 PM, Bart Schaefer wrote: > On Sep 14, 10:06pm, Jesper Nygards wrote: > } Subject: Splitting into a one element array > } I am writing a function where I want to split the $LBUFFER on whitespace, > } and then handle the last element in the resulting array. My initial > attempt > } looked like this: > } > } local dir=${${(z)LBUFFER}[-1]} > } > } This works if $LBUFFER contains more than one word, but fails if it is > one > } word only. > > Yes, this is a side-effect of nested expansion working "inside out"; if > if there's nothing for ${(z)...} to split, the result becomes a scalar > before the context one level up is even considered. > > For the case of splitting on spaces you can fix this with a subscript > flag as in ${LBUFFER[(w)-1]}, but because (z) splits on syntax rather > than on spaces there's no simple corresponding flag for "subscript on > shell words". > > So the most readable way is probably the local array that ZyX suggested, > but if you really want a single nested expansion: > > ${${(z)LBUFFER}[(wps:$'\0':)-1]} > > This works because the subscript flag [(w)] is only active when applied > to a scalar, so (ps:$'\0':) only applies when (z) returns a single word, > which won't contain a $'\0' byte so is not split further. > --089e013d2024396a16051fc31bec--