From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8015 invoked by alias); 14 Sep 2015 21:47:00 -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: 20561 Received: (qmail 20358 invoked from network); 14 Sep 2015 21:46:59 -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 autolearn=ham autolearn_force=no version=3.4.0 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=jB+1rbE4UFK5+YjDnZOVZna54mwxjjfx16+CNlvtyfs=; b=HccH6L/rx4IOohksB2kNheIlg/eDUDMwczRrO9Z0OuzZFW5pJREQr+rsduV+gGf+Jx UN6mBwKM8fjfeFZ3CYqrSBhdwQjx3gprSWF6uAy3A9XFv9Y1H3eIcbq4KMUfCuhxZmj6 5ciwTcYR8gIj1k6gndshaHGFoYXkexqLUBdOPpkLdsYkvn6wHAQf0vZrWD1wl6nvPlPB N05eQRCE/ePQXBgDyvrmg7Bo7Ed47Y+wKa2oyrOWGkKduHfbKe4F8H6cAGa7J0a9KBCR oJvtEBIOe0q+21aReWs83HOBKaGaX0ZF1TihAGHishK78H3Iaru41NHTq3ezYHSU1VE1 GENg== X-Gm-Message-State: ALoCoQlvpqFKwxQWsKO25MyhXxXdRAAKaRRvAyEYrXkKOhqeit9ZrrGd4xQclgY6QhvSU70fWcSN X-Received: by 10.182.96.100 with SMTP id dr4mr14523815obb.49.1442267217818; Mon, 14 Sep 2015 14:46:57 -0700 (PDT) From: Bart Schaefer Message-Id: <150914144654.ZM26119@torch.brasslantern.com> Date: Mon, 14 Sep 2015 14:46:54 -0700 In-Reply-To: Comments: In reply to Jesper Nygards "Splitting into a one element array" (Sep 14, 10:06pm) References: X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Zsh Users Subject: Re: Splitting into a one element array MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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.