zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Splitting into a one element array
Date: Mon, 14 Sep 2015 14:46:54 -0700	[thread overview]
Message-ID: <150914144654.ZM26119@torch.brasslantern.com> (raw)
In-Reply-To: <CABZhJg-AsuXy5AfpOHDhOGzKPaoO2j_uYrSsCtKiTd+oiyX0_g@mail.gmail.com>

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.


  parent reply	other threads:[~2015-09-14 21:47 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 [this message]
2015-09-15  6:15   ` Jesper Nygårds

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=150914144654.ZM26119@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).