zsh-users
 help / color / mirror / code / Atom feed
From: ZyX <kp-pav@yandex.ru>
To: "Jesper Nygårds" <jesper.nygards@gmail.com>,
	"Zsh Users" <zsh-users@zsh.org>
Subject: Re: Splitting into a one element array
Date: Mon, 14 Sep 2015 23:26:13 +0300	[thread overview]
Message-ID: <1115841442262373@web3m.yandex.ru> (raw)
In-Reply-To: <CABZhJg-AsuXy5AfpOHDhOGzKPaoO2j_uYrSsCtKiTd+oiyX0_g@mail.gmail.com>

14.09.2015, 23:07, "Jesper Nygårds" <jesper.nygards@gmail.com>:
> 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. I tested the behavior with the following function:
>
> trysplit() {
>     local astring="aaa bbb"
>     print -l ${=astring}
>     print
>     print -l ${${=astring}[-1]}
>     print
>     local cstring="ccc"
>     print -l ${=cstring}
>     print
>     print -l ${${=cstring}[-1]}
> }
>
> with this result:
>
> % trysplit
> aaa
> bbb
>
> bbb
>
> ccc
>
> c
>
> So, my interpretation of the above is that IF the split results in only one
> word, the result is not handled as an array with one element, but as a
> regular string. And then only the first letter of the $cstring is printed.
>
> Is there some way to handle this that I have missed, or do I need to check
> the split result for size, and then treat the one-element case differently?

I have a workaround for this:

    local -a arr
    arr=( ${(z)LBUFFER} )
    local dir=${arr[-1]}


  reply	other threads:[~2015-09-14 20:32 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 [this message]
2015-09-14 21:46 ` Bart Schaefer
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=1115841442262373@web3m.yandex.ru \
    --to=kp-pav@yandex.ru \
    --cc=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).