From: "Jesper Nygårds" <jesper.nygards@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Splitting into a one element array
Date: Mon, 14 Sep 2015 22:06:17 +0200 [thread overview]
Message-ID: <CABZhJg-AsuXy5AfpOHDhOGzKPaoO2j_uYrSsCtKiTd+oiyX0_g@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 977 bytes --]
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?
next reply other threads:[~2015-09-14 20:06 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-14 20:06 Jesper Nygårds [this message]
2015-09-14 20:26 ` ZyX
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=CABZhJg-AsuXy5AfpOHDhOGzKPaoO2j_uYrSsCtKiTd+oiyX0_g@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).