zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: ZSH User List <zsh-users@sunsite.dk>
Subject: Re: how to force scalar to be an array?
Date: Sat, 28 Aug 2004 11:08:52 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.61.0408280806540.10647@toltec.zanshin.com> (raw)
In-Reply-To: <20040828074355.GA4953@spiegl.de>

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]


  reply	other threads:[~2004-08-29 12:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-27 15:49 Andy Spiegl
2004-08-27 21:15 ` Bart Schaefer
2004-08-27 21:27 ` Bart Schaefer
2004-08-28  7:43   ` Andy Spiegl
2004-08-28 18:08     ` Bart Schaefer [this message]
2004-08-30 16:39       ` Andy Spiegl

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=Pine.LNX.4.61.0408280806540.10647@toltec.zanshin.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /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).