Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: substring extraction



"Stephane Chazelas" <Stephane_Chazelas@xxxxxxxx> wrote:
> In zsh:
>
> $var[2,3]
>
> zsh can do it because arrays and scalar variables are two
> different types. In zsh, $var[2,5] gives the 2nd to 5th element
> when $var is of array type, and 2nd to 5th char when $var is
> scalar. In ksh/bash all variables are arrays and $var is a
> shortcut for ${var[0]} (though it's not completely true of
> bash).

Just to point out you can combine the two in the same expression in zsh.
As soon as you've extracted a single element, later suffixes work on
characters.  (One day this may be multibyte characters but that's not done
yet.)

% array=(one two three four)
% print ${array[3][2,-2]}
hre

Since the rule is applied after every subscript match, you can do bizarre
stuff like:

% print ${array[1,3][-1][2,-2][2]}
r

which works in the following steps:

array  one two three four -> [1,3]
array  one two three -> [-1]
scalar three -> [2,-2]
scalar hre -> [2]
scalar r

--
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



Messages sorted by: Reverse Date, Date, Thread, Author