zsh-users
 help / color / mirror / code / Atom feed
From: "Jesper Nygårds" <jesper.nygards@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Splitting into a one element array
Date: Tue, 15 Sep 2015 08:15:19 +0200	[thread overview]
Message-ID: <CABZhJg95_47G_fVDfsuGpYVUjF_=BK0pS0KRDtB+zj8n63yELA@mail.gmail.com> (raw)
In-Reply-To: <150914144654.ZM26119@torch.brasslantern.com>

[-- Attachment #1: Type: text/plain, Size: 1354 bytes --]

Ah yes, now I see. Thank you both!


On Mon, Sep 14, 2015 at 11:46 PM, Bart Schaefer <schaefer@brasslantern.com>
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.
>

      reply	other threads:[~2015-09-15  6:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-14 20:06 Jesper Nygårds
2015-09-14 20:26 ` ZyX
2015-09-14 21:46 ` Bart Schaefer
2015-09-15  6:15   ` Jesper Nygårds [this message]

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='CABZhJg95_47G_fVDfsuGpYVUjF_=BK0pS0KRDtB+zj8n63yELA@mail.gmail.com' \
    --to=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).