From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21907 invoked by alias); 14 Sep 2015 20:32:22 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20560 Received: (qmail 23605 invoked from network); 14 Sep 2015 20:32:21 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1442262374; bh=rhOZzFXev1oTbov/NEHbp4/V0EKSVBhzy7GXzmnG9zE=; h=From:To:In-Reply-To:References:Subject:Date; b=KBBOhdrzqCwoMH5cvLjutTmGL9TWB4XN9AYja9pgYSaGnPbULxVo0RC9OaVsRdOtO 1P8fXTwmV9bIJ2fwEU5MlJr4kNrrvqZEXyTI/Fu6knRQPY2NiiJjN/B4vvJZ7gPeqV 6JoNAXOxo7F/auck53iuUqUBIugD7vDqNuzQmppw= From: ZyX To: =?utf-8?B?SmVzcGVyIE55Z8OlcmRz?= , Zsh Users In-Reply-To: References: Subject: Re: Splitting into a one element array MIME-Version: 1.0 Message-Id: <1115841442262373@web3m.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Mon, 14 Sep 2015 23:26:13 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=utf-8 14.09.2015, 23:07, "Jesper Nygårds" : > 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]}