Drigan has asked for the wisdom of the Perl Monks concerning the following question:

I have several hundred property files that I pop into a hash that looks like hash{filename}{property} = propertyValue;

I want to ignore any properties that have been added to an 'ignore properties that look like this regex' file. I pop the file into an array, then try to remove the properties that match the regex (pseudocode follows):

my @array = ignorefile.txt
foreach property...
foreach my $item (@array){
if ($property =~ m/$ignore/i){
remove this property
}
}

If the values are:
$item = "words\\:\\:moreWords_\d+";
$property = "words\:\:moreWords_68";

Then this won't match:
if ($property =~ m/$ignore/i){

but this will:
if ($property =~ m/$words\\:\\:moreWords_\d+/i){

Is there a simple way to make them match?

Thanks,
Joe
  • Comment on Searching an array with an array of regexes

Replies are listed 'Best First'.
Re: Searching an array with an array of regexes
by kennethk (Abbot) on Mar 21, 2011 at 16:19 UTC
    I'm a little confused by your spec - in cases like this, inclusion of a bit more of your input and output (preferably wrapped in <code> tags, Markup in the Monastery) might make things more obvious.

    If you want to collect a series of regular expressions to test each line against, I would suggest using an array of regex references - see Regexp Quote Like Operators in perlop. That way each test is independent. I like to use the special variable $" to join the resulting regular expressions with alternators, though you could use grep to test each individually. Perhaps something like:

    #!/usr/bin/perl use strict; use warnings; my @ignore_list = ( qr/^\Qwords\:\:moreWords_\E\d+/i, qr/^[^:]+$/ ); local $" = '|'; while (<DATA>) { print unless /@ignore_list/; } __DATA__ words\:\:moreWords_68 words\:\:moreWords_92 words\:\:fewerWords_92 words\:\:moreWords_a7 birds\:\:moreWords_68 item
Re: Searching an array with an array of regexes
by jethro (Monsignor) on Mar 21, 2011 at 16:48 UTC
    > perl -e '$ignore="words:\\d+"; $property="words:45"; print "yes\n +" if ($property=~m/$ignore/i);' yes

    Notice that I had to double the backslashes of \d, because "" takes away one backslash there too

    You can find those bugs (as I did just now) by making a simple test script and removing "problematic patterns" until your test script works. The last pattern you remove must be the troublemaker