in reply to How to club different lines of program into one

"slurping" in a whole file into a scalar variable sounds like a good idea in this case. $text = <IN>; looks fine to me. There is no need for @text = <IN>.

I would suggest the use of the Perl function index() rather than regex in this situation.

index requires an exact match and so you should case search term and search text to be the same. But this "casing" operation is very fast. The index function will quit on the first match which is an advantage over regex this situation.

As always, your mileage may vary! Short "how to" is shown below.

#!/usr/bin/perl -w use strict; my @listOfWords = qw (january february egypt moon saturday zoos zoo thingies thing ); my $text = "moon. I love full moons but this it has been a long thing since yesterday on the beach. And a whole buch of BLAH.\nYet another february line.\n More jan stuff goes here. What a zoo this text searching thing can be!"; print"\n\nUsing ListOfWord Tokens\n"; foreach my $word (@listOfWords) { if ( index($text,"$word")>= 0) { print "word: $word\t found\n"; } else { print "word: $word\t NOT found\n"; } } __END__ Using ListOfWord Tokens word: january NOT found word: february found word: egypt NOT found word: moon found word: saturday NOT found word: zoos NOT found word: zoo found word: thingies NOT found word: thing found

Replies are listed 'Best First'.
Re^2: How to club different lines of program into one
by akho (Hermit) on May 26, 2009 at 16:41 UTC
    $text = <IN>; will not work like you want it to if you do not undef $/.

    Regexen stop after the first match; you may gain a performance benefit, but not for the reason you cite.

      Thanks for the clarifications!

      As far as performance goes, it probably doesn't make any difference. So here we just have another way of doing things.