my $entity = join '|', keys %entitylist;
$string =~ s/($entity)/$entitylist{$1}/g;
This way you only scan the whole string once instead of once for each entity.
Update: I've gotten a private message that this needs a /e switch to work. I submit that this is not the case.
my %entitylist = ( a => 1, b => 2);
my $string = 'abc';
my $entity = join '|', keys %entitylist;
$string =~ s/($entity)/$entitylist{$1}/g;
print $string, "\n";
__END__
12c
(I actually tested this before my original post, but I only pasted in the relevant portion.) |