zsh-users
 help / color / mirror / code / Atom feed
* pattern matching question
@ 2006-10-11 13:02 arno.
  2006-10-11 13:15 ` Frank Terbeck
  0 siblings, 1 reply; 5+ messages in thread
From: arno. @ 2006-10-11 13:02 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 958 bytes --]

Hello,
I have an array, for example: 

local -a list
list=("... one giraffe ..." "... two cows ..." "... one monkey ..." "...  three lemmings ...")

("..." can represent anything)

and I want to get parts of each item each time a specific pattern 
occurs. For example, I want to get the word after "one". I therefore 
want to finish with an array containing ("giraffe" "monkey")

I found a way with a loop :

local -a secondlist
local item

for item in $list; do
    if [[ $item = (#b)*one\ ([a-z]#)* ]]; then
        secondlist=($secondlist $match[1])
    fi
done
print -l $secondlist

Great, works fine.
But I was wondering if there was something more zsh-ish that could allow 
me to do it in one instruction.

I found :

print -l ${${(M)list##*one ([a-z]#)*}//(#b)*one ([a-z]#)*/$match[1]}

but I think this is inefficient as the pattern has to be evaluated 
twice.

What would be the better way to do that ?

thanks

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: pattern matching question
  2006-10-11 13:02 pattern matching question arno.
@ 2006-10-11 13:15 ` Frank Terbeck
  2006-10-11 13:25   ` Frank Terbeck
  2006-10-11 13:29   ` Peter Stephenson
  0 siblings, 2 replies; 5+ messages in thread
From: Frank Terbeck @ 2006-10-11 13:15 UTC (permalink / raw)
  To: zsh-users

arno. <arno.@no-log.org>:
> I have an array, for example: 
> 
> local -a list
> list=("... one giraffe ..." "... two cows ..." "... one monkey ..." "...  three lemmings ...")
> 
> ("..." can represent anything)
> 
> and I want to get parts of each item each time a specific pattern 
> occurs. For example, I want to get the word after "one". I therefore 
> want to finish with an array containing ("giraffe" "monkey")
[...]
> What would be the better way to do that ?

[snip]
list=("... one giraffe ..." "... two cows ..." "... one monkey ..." "... three lemmings ...")
slist=(${(M)list:#*one [a-z]*})
print old:
print -l $list
print "\nnew:"
print -l $slist
[snap]

Regards, Frank


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

* Re: pattern matching question
  2006-10-11 13:15 ` Frank Terbeck
@ 2006-10-11 13:25   ` Frank Terbeck
  2006-10-11 13:29   ` Peter Stephenson
  1 sibling, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2006-10-11 13:25 UTC (permalink / raw)
  To: zsh-users

Frank Terbeck <ft@bewatermyfriend.de>:
> [snip]
> list=("... one giraffe ..." "... two cows ..." "... one monkey ..." "... three lemmings ...")

Sorry, for not reading your post close enough:
> slist=(${(M)list:#*one [a-z]*})

slist=(${${${(M)list:#*one [a-z]*}##*one }%%[^a-z]*})

> print old:
> print -l $list
> print "\nnew:"
> print -l $slist
> [snap]

Regards, Frank


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

* Re: pattern matching question
  2006-10-11 13:15 ` Frank Terbeck
  2006-10-11 13:25   ` Frank Terbeck
@ 2006-10-11 13:29   ` Peter Stephenson
  2006-10-11 14:08     ` arno.
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2006-10-11 13:29 UTC (permalink / raw)
  To: zsh-users

Frank Terbeck wrote:
> list=("... one giraffe ..." "... two cows ..." "... one monkey ..." "... thre
> e lemmings ...")
> slist=(${(M)list:#*one [a-z]*})

To get the exact effect arno wants, you need an additional substitution
to remove the extra stuff, which brings it back to his original
proposal.  I'd probably just do the simple

slist=(${${${(M)list:#*one [a-z]*}##*one }%% *})

which now has two extra pattern matches.

I can't offhand think of a way of both matching the one and extracting
the following word in one go.  In practice it's unlikely to make a
noticeable difference unless you're really doing this a great deal---and
if speed's that important it's probably time to switch to a more
optimized scripting language.

-- 
Peter Stephenson <pws@csr.com>                  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


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

* Re: pattern matching question
  2006-10-11 13:29   ` Peter Stephenson
@ 2006-10-11 14:08     ` arno.
  0 siblings, 0 replies; 5+ messages in thread
From: arno. @ 2006-10-11 14:08 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1083 bytes --]

Le Wednesday 11 October 2006, à 14:29:06PM +0100, Peter a écrit :
> Frank Terbeck wrote:
> > list=("... one giraffe ..." "... two cows ..." "... one monkey ..." "... thre
> > e lemmings ...")
> > slist=(${(M)list:#*one [a-z]*})
>
> To get the exact effect arno wants, you need an additional substitution
> to remove the extra stuff, which brings it back to his original
> proposal.  I'd probably just do the simple
>
> slist=(${${${(M)list:#*one [a-z]*}##*one }%% *})

Thanks to you and frank for your answers, that works fine.

>
> I can't offhand think of a way of both matching the one and extracting
> the following word in one go.  In practice it's unlikely to make a
> noticeable difference unless you're really doing this a great deal---and
> if speed's that important it's probably time to switch to a more
> optimized scripting language.

I don't really need that much speed, I was just wondering if was
possible to match and extract in one go. Anyway, using another language
is not an option here: I'm trying to write a completion function.

arno

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

end of thread, other threads:[~2006-10-11 14:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-11 13:02 pattern matching question arno.
2006-10-11 13:15 ` Frank Terbeck
2006-10-11 13:25   ` Frank Terbeck
2006-10-11 13:29   ` Peter Stephenson
2006-10-11 14:08     ` arno.

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