I want to illustrate an iterative solution that uses map and grep. Admittedly this method puts a loop inside of a loop (rather than just one loop). Here goes.....

my @lines = qw/marge homer bart jerry maggie george elaine kramer lisaross chandler fibi joey monica rachel batman robin catwoman batgirl superman thetick hollywood studio universal compton harbor downtown monica venice oc oxnard ventura oaks/; my @kwords = qw/bart maggie monica pete/; my @matches = map { my $kword=$_; grep /\b$kword\b/, @lines } @kwords; { local $, = "\t"; print @matches; }

This is just an example. A little tinkering will match it to your needs. Enjoy!

Update: I tested the above method against the regexp method in the first followup in this thread. The regexp (alternation) method proved to be faster for this small test-set of data. Even when I grew the keyword list to about 40 words, and the array of strings to find keywords in to about 40 lines of 10 "words" each, I still found alternation to be faster. I don't know what to expect if the keyword list gets REALLY big. Obviously my method puts a loop inside a loop. But alternation itself isn't considered terribly fast. Someone with a lot of test data might be able to shed light.


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

In reply to Re: How can I grep these IP-addresses from these logfiles? by davido
in thread How can I grep these IP-addresses from these logfiles? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.