http://www.perlmonks.org?node_id=1055871


in reply to Re^4: using hashes
in thread iterating hash keys?

That is a trickier proposition. If you know your words are separated by word boundaries, you can solve your issue with the \b assertion:
s/\b$find\b/$replace/g
That will require that either side of your key must correspond to the start or finish of the line, or to the pattern \w\W or \W\w.

If you can't use word boundaries to delimit, you could try your keys in descending key length.

for my $line (@lines) { for my $find(sort {length($b) <=> length($a)} keys %ids) { $line =~ s/$find/$ids{$find}/g; } }

This still has some potential for key overlap, but we'd really need to see you real data to design the correct regex for it. There are a number of ways to cut this up, the choosing the 'right' answer depends strongly on actual input.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.