in reply to Count replaces in search and replace

Thanks, the final solution I went with is:

my $YESMapping = 0; foreach my $replace (keys(%map)) { $YESMapping = $YESMapping + ($_ =~ s/$replace/$map{$replace}/gi) } print "Replaced $YESMapping entries.\n"

I did some reading on regex compiling and I'm looking at how to maybe apply that to the $replace variable which is obviously my loop index.

Replies are listed 'Best First'.
Re^2: Count replaces in search and replace
by kennethk (Abbot) on Mar 12, 2009 at 15:42 UTC

    One final comment - perhaps you should consider the += operator? As well, since you are visiting each key-value pair once, each might be appropriate. And, as long as you are using $_, you may way well use its shorthand.

    my $YESMapping = 0; while (my ($key, $value) = each %map) { $YESMapping += s/$key/$value/gi; } print "Replaced $YESMapping entries.\n"

      Yeah, I realized the += mistake (as in I should have used that) after I had posted it and saw it in front of me. I went back and changed my actual code.

      The "each" recommendation is new to me. I'm certainly not a Perl expert (yet), so I rely on the faithful "foreach" when I want to do something "for each" member of a group. Thanks for the tip, I'll read up on that and try it out.