From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7646 invoked from network); 29 Aug 2004 12:27:44 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 29 Aug 2004 12:27:44 -0000 Received: (qmail 44223 invoked from network); 29 Aug 2004 12:27:37 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 29 Aug 2004 12:27:37 -0000 Received: (qmail 21026 invoked by alias); 28 Aug 2004 18:23:15 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7943 Received: (qmail 21016 invoked from network); 28 Aug 2004 18:23:14 -0000 Received: from unknown (HELO a.mx.sunsite.dk) (130.225.247.88) by 130.225.247.90 with SMTP; 28 Aug 2004 18:23:14 -0000 Received: (qmail 93950 invoked from network); 28 Aug 2004 18:08:56 -0000 Received: from moonbase.zanshin.com (64.84.47.139) by a.mx.sunsite.dk with SMTP; 28 Aug 2004 18:08:55 -0000 Received: from toltec.zanshin.com (toltec.zanshin.com [64.84.47.166]) by moonbase.zanshin.com (8.13.1/8.13.1) with ESMTP id i7SI8qlq014500 for ; Sat, 28 Aug 2004 11:08:52 -0700 Date: Sat, 28 Aug 2004 11:08:52 -0700 (PDT) From: Bart Schaefer Reply-To: zsh-users@sunsite.dk To: ZSH User List Subject: Re: how to force scalar to be an array? In-Reply-To: <20040828074355.GA4953@spiegl.de> Message-ID: References: <20040827154905.GA26846@spiegl.de> <20040828074355.GA4953@spiegl.de> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Checker-Version: SpamAssassin 2.63 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, hits=0.0 required=6.0 tests=none autolearn=no version=2.63 X-Spam-Hits: 0.0 On Sat, 28 Aug 2004, Andy Spiegl wrote: > > So your only safe bet is to actually assign the scalar to an array, and > > then use subscripting on the array. > But then how would I be able to split the words if there are more than one? What I meant was: words=( $=result ) print $words[1] You can even do it in one expression: print ${${(A)=words::=$result}[1]} Note that you have to put the splitting flag on the LEFT side of ::= or you don't get the array behavior from the subscript when there is only one element. That works with the (s) parameter flag and its relatives, too; compare: print ${${(As:,:)words::=chop,split,slice}[1]} print ${${(As:.:)words::=chop,split,slice}[1]} > Hm, I think I must be doing something wrong here. Shouldn't it be an > absolutely easy and common task to split a line into its words and then > pick the first one and the rest of them. Yes, you'd think so, but it's been zsh's behavior for a long time to treat a one-element array as a scalar in certain contexts. You'd also think that the (@) flag would fix that, but it only *preserves* array-ness in the transition from inner to outer nested expansions, it doesn't *create* array-ness from a scalar. The latter might be something we could consider changing without much risk of breaking anything. > In Perl I'd do for example: (just to illustrate what I want to do) > $result=`some_proggie` > @words = split (/\s/, $result); > or maybe: > $result =~ /(\S+)(.*)/; > $first_word=$1; > $rest_words=$2; You can do that latter one in zsh, too, if you have extendedglob set: : ${result//(#b)([^[:space:]]#)(*)/} print -l $match[1] $match[2]