use strict; use File::Basename; my %count; my @mask = ( '/some/path/', 'some/other/path', 'another/path', ); my $regex = join '|', @mask; # Now you have a list of alternatives $regex = qr # now the regex is precompiled # N.B. I added \b to make the filename only match on word boundaries. # So some/path won't match anothersome/path # but it will match another/some/path open F, "access.log"; while () { if (/$regex/) { ++$count{basename($1)}; } } foreach (sort keys %count) { print $_," = ",$count{$_},"\n"; }