zsh-users
 help / color / mirror / code / Atom feed
* wrong array range with (r) flag on scalar?
@ 2015-03-05  8:30 Han Pingtian
  2015-03-05 16:44 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Han Pingtian @ 2015-03-05  8:30 UTC (permalink / raw)
  To: zsh-users

Hi,

Looks like this is wrong:

localhost% a='hello world'
localhost% print $a[(r)h?]
h
localhost% print $a[(r)h?,(r)w?]
hello wo
localhost%

In the range, the end should be "w" and the result should be "hello w"?


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

* Re: wrong array range with (r) flag on scalar?
  2015-03-05  8:30 wrong array range with (r) flag on scalar? Han Pingtian
@ 2015-03-05 16:44 ` Bart Schaefer
  2015-03-05 22:16   ` Roman Neuhauser
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2015-03-05 16:44 UTC (permalink / raw)
  To: zsh-users

On Mar 5,  4:30pm, Han Pingtian wrote:
} Subject: wrong array range with (r) flag on scalar?
}
} Hi,
} 
} Looks like this is wrong:
} 
} localhost% a='hello world'
} localhost% print $a[(r)h?]
} h
} localhost% print $a[(r)h?,(r)w?]
} hello wo
} localhost%
} 
} In the range, the end should be "w" and the result should be "hello w"?

The range goes from the start of the left pattern to the end of the
shortest match for the second pattern.  This is the only thing that
makes sense if the range is going to be "inclusive" of the end of the
pattern match.  With (R), it's the end of the longest match for the
second pattern.

Would you expect

torch% print $a[(r)h?,(r)wo] 
hello wo

to stop at the "w"?  

The doc could certainly be more clear about this.


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

* Re: wrong array range with (r) flag on scalar?
  2015-03-05 16:44 ` Bart Schaefer
@ 2015-03-05 22:16   ` Roman Neuhauser
  0 siblings, 0 replies; 3+ messages in thread
From: Roman Neuhauser @ 2015-03-05 22:16 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

# schaefer@brasslantern.com / 2015-03-05 08:44:08 -0800:
> Would you expect
> 
> torch% print $a[(r)h?,(r)wo] 
> hello wo
> 
> to stop at the "w"?  

yes, but only *after* reading this carefully:

  r  Reverse subscripting: if this flag is given, the exp is taken as
     a  pattern  and  the result is the first matching array element,
     substring or word (if the parameter is an  array,  if  it  is  a
     scalar,  or if it is a scalar and the `w' flag is given, respec-
     tively).  The subscript used is the number of the matching  ele-
     ment,  so  that  pairs of subscripts such as `$foo[(r)??,3]' and
     `$foo[(r)??,(r)f*]' are possible if  the  parameter  is  not  an
     associative  array.

note: "The subscript used is the number of the matching  element".

i would definitely expect a (0-based indexing, pseudocode)
`substr foobar bar` to be 3, not 5.

> The doc could certainly be more clear about this.

oh yeah. :)

-- 
roman


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

end of thread, other threads:[~2015-03-05 22:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-05  8:30 wrong array range with (r) flag on scalar? Han Pingtian
2015-03-05 16:44 ` Bart Schaefer
2015-03-05 22:16   ` Roman Neuhauser

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