Matching an array of strings to strings in a file.

Gianni Ceccarelli dakkar at
Mon Aug 29 13:13:11 BST 2011

On 2011-08-29 "Martin A. Brooks" <martin at> wrote:
> I have an array of strings.  I have some plain text files on disk.  I
> want to see if any of the lines in the text files contain any of the
> strings in the array.  I can stop as soon as I find one match.

use Test::More;

sub look_in_file {
    my ($fh,$strings) = @_;

    my $rx = join '|',map { "\Q$_\E" } @$strings;
    $rx = qr{$rx};

    while (my $line = <$fh>) {
        #warn "<$line> =~ /$rx/\n";
        return 1 if $line =~ $rx;

my $p=tell(DATA);
ok(look_in_file(\*DATA,[qw(foo bar baz)]));
seek DATA,$p,0;

this line is useless
this one says bar
this one says foo, but should be ignored

	Dakkar - <Mobilis in mobile>
	GPG public key fingerprint = A071 E618 DD2C 5901 9574
	                             6FE2 40EA 9883 7519 3F88
	                    key id = 0x75193F88

 Hermes: Baby needs a new pair of shoes! 
 Zoidberg: To hell with your spoiled baby, I need those shoes. 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
Url :

More information about the mailing list