Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Splitting into a one element array
- X-seq: zsh-users 20564
- From: Jesper Nygårds <jesper.nygards@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Splitting into a one element array
- Date: Tue, 15 Sep 2015 08:15:19 +0200
- 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==
- In-reply-to: <150914144654.ZM26119@torch.brasslantern.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>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CABZhJg-AsuXy5AfpOHDhOGzKPaoO2j_uYrSsCtKiTd+oiyX0_g@mail.gmail.com> <150914144654.ZM26119@torch.brasslantern.com>
Ah yes, now I see. Thank you both!
On Mon, Sep 14, 2015 at 11:46 PM, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
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.
>
Messages sorted by:
Reverse Date,
Date,
Thread,
Author