Article 2785 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:2785
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!wupost!howland.reston.ans.net!agate!ames!sgi!wdl1!wdl39!mab
From: mab@wdl39.wdl.loral.com (Mark A Biggar)
Subject: Re: A Faster Grep???
Message-ID: <1993May14.155157.26315@wdl.loral.com>
Sender: news@wdl.loral.com
Organization: Loral Western Development Labs
References: <2BF2B834.6812@ics.uci.edu>
Date: Fri, 14 May 1993 15:51:57 GMT
Lines: 40

In article <2BF2B834.6812@ics.uci.edu> kvashi@vlsi.ics.uci.edu (Kevin Vashi) writes:
>   I'm reading a file which has the following content:
>/usr/lib/something_unique1
>/usr/bin/something_unique2
>/usr/lib/something_unique3
>/usr/lib/something_unique4
>/usr/bin/something_unique5
>and so on...
>Now I'd like to have lists which have the same path before something unique.
>I'l like List A to have all the filess+path in /usr/bin and
>list B to have all the files+path in /usr/lib.
>At present I'm using the following piece of code that uses grep
>   while(<>)
>   {
>      @listA = grep(/$path1/,@path_array);
>      @listB = grep(/$path2/,@path_array);
>   }
>Is there anyway I could use associative arrays to speed this code???

I don't think that associative arrays would help, but it can be done
using only 1 grep instead of two:

while(<>) {
     @listA = ();
     @listB = grep(/$path2/ || (/$path1/ && (push(listA,$_),0)), @path_array);
}

If the line matches /$path2/ then the control expression is true and the
line will go into listB, otherwise if /$path1/ then we push the line on listA
and return 0 so the line doesn't end up in listB also.  In addition, lines
that don't match either pattern are ignored.  If your code only does the above
operation once then both patterns should have an o modifier.  If you do it
more then once, you probably want to wrap the grep in an eval to preexpand 
the patterns.

--
Perl's Maternal Uncle
Mark Biggar
mab@wdl1.wdl.loral.com