in reply to Map function slowing down program

You can use the fact that map returns a value:

my $cnt = map $line =~ /$_/, keys %href;

Replies are listed 'Best First'.
Re^2: Map function slowing down program
by mpeever (Friar) on Nov 17, 2008 at 03:46 UTC

    Absolutely! Using map purely for the side-effect of incrementing a counter is a little obtuse. The code is a lot cleaner using the return value of map (or grep).

Re^2: Map function slowing down program
by AnomalousMonk (Archbishop) on Nov 17, 2008 at 03:05 UTC
    That should be grep (I also favor using an array rather than a hash):
    >perl -wMstrict -le "my @strs = qw( ab cd ef gh ); my @rxs = map qr{ \Q$_\E }xms, @strs; my $line = 'xxabyyefzzghhh'; my $cnt = grep $line =~ $_, @rxs; print $cnt; " 3

      It works with map as well as with grep.   Try it and see.

        Yes and no. With map, it depends on the pattern.
        >perl -le"@rxs='(.)(.)'; $line = 'ab'; my $cnt = grep $line =~ $_, @rx +s; print $cnt" 1 >perl -le"@rxs='(.)(.)'; $line = 'ab'; my $cnt = map $line =~ $_, @rxs +; print $cnt" 2

        grep is more robust. It'll work with just about any expression, whereas map only works with very specific expressions.