zsh-users
 help / color / mirror / code / Atom feed
* how to force scalar to be an array?
@ 2004-08-27 15:49 Andy Spiegl
  2004-08-27 21:15 ` Bart Schaefer
  2004-08-27 21:27 ` Bart Schaefer
  0 siblings, 2 replies; 6+ messages in thread
From: Andy Spiegl @ 2004-08-27 15:49 UTC (permalink / raw)
  To: ZSH User List

Sorry, guys, one more question:

I've got this:
  result=`some_proggie`
  first_word=${${=result}[1]}
  rest_words=${${=result}[2,-1]}
But this works as expected only if result contains more than one word.

An example:
 $ result="foo bar baz"
 $ print -c ${${=result}[1]}
 foo

 $ result="foo"             
 $ print -c ${${=result}[1]}
 f

I also tried this:
 $ print -c ${${(@)=result}[1]}
 f

What am I doing wrong here?

Oh, boy what a day!  :-(
 Andy.

-- 
                              o      _     _         _
  ------- __o       __o      /\_   _ \\o  (_)\__/o  (_)          -o)
  ----- _`\<,_    _`\<,_    _>(_) (_)/<_    \_| \   _|/' \/       /\\
  ---- (_)/ (_)  (_)/ (_)  (_)        (_)   (_)    (_)'  _\o_    _\_v
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Warning: This email, when printed on paper, has sharp edges.
 Handle with care or serious injury may result.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to force scalar to be an array?
  2004-08-27 15:49 how to force scalar to be an array? Andy Spiegl
@ 2004-08-27 21:15 ` Bart Schaefer
  2004-08-27 21:27 ` Bart Schaefer
  1 sibling, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2004-08-27 21:15 UTC (permalink / raw)
  To: ZSH User List

On Fri, 27 Aug 2004, Andy Spiegl wrote:

> Sorry, guys, one more question

I get "foo" from all three of your examples ... in any of zsh versions 
4.2.0, 4.0.7, and 3.0.7.

So there must be something else going on.

Can you reproduce with "zsh -f"?


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to force scalar to be an array?
  2004-08-27 15:49 how to force scalar to be an array? Andy Spiegl
  2004-08-27 21:15 ` Bart Schaefer
@ 2004-08-27 21:27 ` Bart Schaefer
  2004-08-28  7:43   ` Andy Spiegl
  1 sibling, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2004-08-27 21:27 UTC (permalink / raw)
  To: ZSH User List

I'm sorry, I missed the second (result="foo") assignment when I was trying 
the examples, so I thought you always had a multi-word scalar to start 
with.  Ignore my previous message.

A scalar is a scalar, and the result of splitting a scalar is only an 
array if the split actually does something.

Further, using $=array forces the array to be a scalar and then applies
the preceding rule.

So your only safe bet is to actually assign the scalar to an array, and
then use subscripting on the array.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to force scalar to be an array?
  2004-08-27 21:27 ` Bart Schaefer
@ 2004-08-28  7:43   ` Andy Spiegl
  2004-08-28 18:08     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Andy Spiegl @ 2004-08-28  7:43 UTC (permalink / raw)
  To: ZSH User List

> 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?
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.

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;

-- 
                              o      _     _         _
  ------- __o       __o      /\_   _ \\o  (_)\__/o  (_)          -o)
  ----- _`\<,_    _`\<,_    _>(_) (_)/<_    \_| \   _|/' \/       /\\
  ---- (_)/ (_)  (_)/ (_)  (_)        (_)   (_)    (_)'  _\o_    _\_v
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 When women are depressed they either eat or go shopping.
 Men invade another country.  --- Elayne Boosler


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to force scalar to be an array?
  2004-08-28  7:43   ` Andy Spiegl
@ 2004-08-28 18:08     ` Bart Schaefer
  2004-08-30 16:39       ` Andy Spiegl
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2004-08-28 18:08 UTC (permalink / raw)
  To: ZSH User List

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]


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: how to force scalar to be an array?
  2004-08-28 18:08     ` Bart Schaefer
@ 2004-08-30 16:39       ` Andy Spiegl
  0 siblings, 0 replies; 6+ messages in thread
From: Andy Spiegl @ 2004-08-30 16:39 UTC (permalink / raw)
  To: zsh-users

Hi Bart,

> What I meant was:
> 
> 	words=( $=result )
> 	print $words[1]
Ah!  Thanks so much once again for your help.

> You can even do it in one expression:
> 
> 	print ${${(A)=words::=$result}[1]}
Cool.  But hard to read.

> 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.
Yep, I did think that.  So maybe your suggestion below would be worth
discussion on zsh-workers. :-)

> The latter might be something we could consider changing without much risk
> of breaking anything.

> You can do that latter one in zsh, too, if you have extendedglob set:
> 
> 	: ${result//(#b)([^[:space:]]#)(*)/}
> 	print -l $match[1] $match[2]
Uffda, gotta go back and read more of the zsh manual.

Thanks again, I really appreciate your help!
 Andy.

-- 
                              o      _     _         _
  ------- __o       __o      /\_   _ \\o  (_)\__/o  (_)          -o)
  ----- _`\<,_    _`\<,_    _>(_) (_)/<_    \_| \   _|/' \/       /\\
  ---- (_)/ (_)  (_)/ (_)  (_)        (_)   (_)    (_)'  _\o_    _\_v
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Life is like a box of chocolates.
 You never know what you get.  (Forrest Gump, 1994)


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-08-30 16:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-27 15:49 how to force scalar to be an array? 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
2004-08-30 16:39       ` Andy Spiegl

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).