zsh-users
 help / color / mirror / code / Atom feed
* Subscripting without temporaries
@ 2004-04-13 12:00 DervishD
  2004-04-13 15:29 ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: DervishD @ 2004-04-13 12:00 UTC (permalink / raw)
  To: Zsh Users

    Hi all :)

    I have an array containing strings of this type, one per 'slot':

    "Some text [particular text] Another text"

    For each line I want to be able to separate the three parts: the
'Some text', the '[Particular text]', and the 'Another text'. I know
I can do it using things like

    print ${array[EXP]/(#b)(*) (\[*\]) (*)/}

    and I will get the three parts in the $match array, one per slot.
I can do this because I know that the brackets can be used as
separators (they are never present in the first part nor the last
part). The question is that I'm sure that there is another better
method for doing the same without using backreferences and parameter
replacement. I think that, as we say in spanish, I'm using a cannon
to kill flies...

    Which is the zsh-cool-way of doing the same?

    Thanks in advance :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Subscripting without temporaries
  2004-04-13 12:00 Subscripting without temporaries DervishD
@ 2004-04-13 15:29 ` Bart Schaefer
  2004-04-13 16:40   ` Lloyd Zusman
  2004-04-13 17:14   ` DervishD
  0 siblings, 2 replies; 11+ messages in thread
From: Bart Schaefer @ 2004-04-13 15:29 UTC (permalink / raw)
  To: Zsh Users

On Apr 13,  2:00pm, DervishD wrote:
> 
>     I have an array containing strings of this type, one per 'slot':
> 
>     "Some text [particular text] Another text"
> 
>     For each line I want to be able to separate the three parts: the
> 'Some text', the '[Particular text]', and the 'Another text'.

>     Which is the zsh-cool-way of doing the same?

Actually I think backreferences in pattern matching is a pretty cool bit
already.

However, as you can treat strings as arrays and index them by character,
and also do slices with pairs of indices:

txt="Some text [particular text] Another text"
print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]


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

* Re: Subscripting without temporaries
  2004-04-13 15:29 ` Bart Schaefer
@ 2004-04-13 16:40   ` Lloyd Zusman
  2004-04-13 17:11     ` Phil Pennock
                       ` (3 more replies)
  2004-04-13 17:14   ` DervishD
  1 sibling, 4 replies; 11+ messages in thread
From: Lloyd Zusman @ 2004-04-13 16:40 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> On Apr 13,  2:00pm, DervishD wrote:
>> 
>>     I have an array containing strings of this type, one per 'slot':
>> 
>>     "Some text [particular text] Another text"
>> 
>>     For each line I want to be able to separate the three parts: the
>> 'Some text', the '[Particular text]', and the 'Another text'.
>
>>     Which is the zsh-cool-way of doing the same?
>
> Actually I think backreferences in pattern matching is a pretty cool bit
> already.
>
> However, as you can treat strings as arrays and index them by character,
> and also do slices with pairs of indices:
>
> txt="Some text [particular text] Another text"
> print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]

This is cool.  But what options are necessary in order to make this
work?  The commands above produce this output for me:

  Some
  text
  [particular
  text]
  Another
  text

This is the same as what I get with this:

  print -l $txt

Thanks.

-- 
 Lloyd Zusman
 ljz@asfast.com


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

* Re: Subscripting without temporaries
  2004-04-13 16:40   ` Lloyd Zusman
@ 2004-04-13 17:11     ` Phil Pennock
  2004-04-13 18:17       ` Lloyd Zusman
  2004-04-13 17:17     ` DervishD
                       ` (2 subsequent siblings)
  3 siblings, 1 reply; 11+ messages in thread
From: Phil Pennock @ 2004-04-13 17:11 UTC (permalink / raw)
  To: zsh-users

On 2004-04-13 at 12:40 -0400, Lloyd Zusman wrote:
> > txt="Some text [particular text] Another text"
> > print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]
> 
> This is cool.  But what options are necessary in order to make this
> work?  The commands above produce this output for me:
> 
>   Some
>   text
>   [particular
>   text]
>   Another
>   text
> 
> This is the same as what I get with this:
> 
>   print -l $txt

That means that you have SH_WORD_SPLIT turned on, which is not a zsh
default.

If turning sh_word_split off is not an option, then use the '='
parameter expansion modifier twice:

 print -l $==txt[1,$txt[(i)\[]-2] $==txt[(r)\[,(R)\]] $==txt[$txt[(I)\]]+2,-1]

-Phil


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

* Re: Subscripting without temporaries
  2004-04-13 15:29 ` Bart Schaefer
  2004-04-13 16:40   ` Lloyd Zusman
@ 2004-04-13 17:14   ` DervishD
  2004-04-14  1:58     ` Bart Schaefer
  1 sibling, 1 reply; 11+ messages in thread
From: DervishD @ 2004-04-13 17:14 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> >     I have an array containing strings of this type, one per 'slot':
> >     "Some text [particular text] Another text"
> >     For each line I want to be able to separate the three parts: the
> > 'Some text', the '[Particular text]', and the 'Another text'.
> >     Which is the zsh-cool-way of doing the same?
> However, as you can treat strings as arrays and index them by character,
> and also do slices with pairs of indices:

    OK, but then I need a temporary... Before the backreference trick
I tried the same using more or less what you suggests below, but I
didn't get far because I cannot do $array[EXP][Whatever] (well,
namely I was trying ${array[10]}[1,8], but zsh told me 'no matches
found').

> txt="Some text [particular text] Another text"
> print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]

    How can I do the same without the temporary? I mean, without
assigning to 'txt' or whatever the name of the temporary the result
of $array[EXP]

    Thanks for your help :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Subscripting without temporaries
  2004-04-13 16:40   ` Lloyd Zusman
  2004-04-13 17:11     ` Phil Pennock
@ 2004-04-13 17:17     ` DervishD
  2004-04-13 17:18     ` Bart Schaefer
  2004-04-13 19:00     ` Wayne Davison
  3 siblings, 0 replies; 11+ messages in thread
From: DervishD @ 2004-04-13 17:17 UTC (permalink / raw)
  To: Lloyd Zusman; +Cc: zsh-users

    Hi Lloyd :)

 * Lloyd Zusman <ljz@asfast.com> dixit:
> > However, as you can treat strings as arrays and index them by character,
> > and also do slices with pairs of indices:
> > txt="Some text [particular text] Another text"
> > print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]
> This is cool.  But what options are necessary in order to make this
> work?  The commands above produce this output for me:
> 
>   Some
>   text
>   [particular
>   text]
>   Another
>   text
> 
> This is the same as what I get with this:
> 
>   print -l $txt

    You have shwordsplit set. Unset it. Believe me, is much better
that way if you work with arrays, parameters and the like ;)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

* Re: Subscripting without temporaries
  2004-04-13 16:40   ` Lloyd Zusman
  2004-04-13 17:11     ` Phil Pennock
  2004-04-13 17:17     ` DervishD
@ 2004-04-13 17:18     ` Bart Schaefer
  2004-04-13 19:00     ` Wayne Davison
  3 siblings, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2004-04-13 17:18 UTC (permalink / raw)
  To: zsh-users

On Apr 13, 12:40pm, Lloyd Zusman wrote:
}
} > txt="Some text [particular text] Another text"
} > print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]
} 
} This is cool.  But what options are necessary in order to make this
} work?  The commands above produce this output for me:
} 
}   Some
}   text
}   [particular
}   text]
}   Another
}   text

unsetopt shwordsplit

or place each of the $txt[...] in double quotes:

print -l "$txt[1,$txt[(i)\[]-2]" "$txt[(r)\[,(R)\]]" "$txt[$txt[(I)\]]+2,-1]"

You'll find that many examples posted to zsh-users assume that arrays are
superior to strings-automatically-split-at-spaces, and therefore assume
that you're using the default zsh setting of no_shwordsplit.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: Subscripting without temporaries
  2004-04-13 17:11     ` Phil Pennock
@ 2004-04-13 18:17       ` Lloyd Zusman
  0 siblings, 0 replies; 11+ messages in thread
From: Lloyd Zusman @ 2004-04-13 18:17 UTC (permalink / raw)
  To: zsh-users

Phil Pennock <phil.pennock@globnix.org> writes:

> On 2004-04-13 at 12:40 -0400, Lloyd Zusman wrote:
>> > txt="Some text [particular text] Another text"
>> > print -l $txt[1,$txt[(i)\[]-2] $txt[(r)\[,(R)\]] $txt[$txt[(I)\]]+2,-1]
>> 
>> This is cool.  But what options are necessary in order to make this
>> work?  The commands above produce this output for me:
>> 
>>   Some
>>   text
>>   [particular
>>   text]
>>   Another
>>   text
>> 
>> This is the same as what I get with this:
>> 
>>   print -l $txt
>
> That means that you have SH_WORD_SPLIT turned on, which is not a zsh
> default.

I'm not sure why, but I had long ago turned on SH_WORD_SPLIT and forgot
about it.  I turned it off, and now this works as advertised.  Thanks!



> [ ... ]

-- 
 Lloyd Zusman
 ljz@asfast.com


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

* Re: Subscripting without temporaries
  2004-04-13 16:40   ` Lloyd Zusman
                       ` (2 preceding siblings ...)
  2004-04-13 17:18     ` Bart Schaefer
@ 2004-04-13 19:00     ` Wayne Davison
  3 siblings, 0 replies; 11+ messages in thread
From: Wayne Davison @ 2004-04-13 19:00 UTC (permalink / raw)
  To: Lloyd Zusman; +Cc: zsh-users

On Tue, Apr 13, 2004 at 12:40:39PM -0400, Lloyd Zusman wrote:
> But what options are necessary in order to make this
> work?  The commands above produce this output for me:
> 
>   Some
>   text
>   [particular
>   text]
>   Another
>   text

You must have sh_word_split set.  This tells zsh to do word-splitting
of variables, even when not explicitly asked to split things (like the
Bourne shell does).  You can either turn this off, or put double quotes
around any variable that you don't want to be split.

..wayne..


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

* Re: Subscripting without temporaries
  2004-04-13 17:14   ` DervishD
@ 2004-04-14  1:58     ` Bart Schaefer
  2004-04-14 16:13       ` DervishD
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2004-04-14  1:58 UTC (permalink / raw)
  To: Zsh Users

On Apr 13,  7:14pm, DervishD wrote:
>     Hi Bart :)

'Lo.

> I tried the same using more or less what you suggests below, but I
> didn't get far because I cannot do $array[EXP][Whatever] (well,
> namely I was trying ${array[10]}[1,8], but zsh told me 'no matches
> found').

Heh.  You *can* do ${array[EXP][Whatever]},
or you can do ${${array[EXP]}[Whatever]},
but you can't do what you said you tried.

schaefer[228] array=( "this text [that text] those texts"
array> "more text [less text] some text"
array> "any text [other text] neither text" )
schaefer[229] print ${array[2][(r)\[,(R)\]]}
[less text]

Also there was a bug until recently where if EXP is a negative number,
then the result of $array[EXP] is still an array rather than a string,
so then the [Whatever] fails to slice the string.


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

* Re: Subscripting without temporaries
  2004-04-14  1:58     ` Bart Schaefer
@ 2004-04-14 16:13       ` DervishD
  0 siblings, 0 replies; 11+ messages in thread
From: DervishD @ 2004-04-14 16:13 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> > I tried the same using more or less what you suggests below, but I
> > didn't get far because I cannot do $array[EXP][Whatever] (well,
> > namely I was trying ${array[10]}[1,8], but zsh told me 'no matches
> > found').
> Heh.  You *can* do ${array[EXP][Whatever]},
> or you can do ${${array[EXP]}[Whatever]},
> but you can't do what you said you tried.

    S**t, how embarrasing... I didn't even try those two
combinations. Well, that way I don't need a temporary.

> Also there was a bug until recently where if EXP is a negative number,
> then the result of $array[EXP] is still an array rather than a string,
> so then the [Whatever] fails to slice the string.

    I'm planning to upgrade to Zsh 4.2.x as soon as the next version
is releases, so I hope I don't get bite by that bug in the interim.

    Thanks a LOT for your help :)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


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

end of thread, other threads:[~2004-04-14 18:06 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-13 12:00 Subscripting without temporaries DervishD
2004-04-13 15:29 ` Bart Schaefer
2004-04-13 16:40   ` Lloyd Zusman
2004-04-13 17:11     ` Phil Pennock
2004-04-13 18:17       ` Lloyd Zusman
2004-04-13 17:17     ` DervishD
2004-04-13 17:18     ` Bart Schaefer
2004-04-13 19:00     ` Wayne Davison
2004-04-13 17:14   ` DervishD
2004-04-14  1:58     ` Bart Schaefer
2004-04-14 16:13       ` DervishD

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