"wana" <> wrote in message
news: om...
> foreach (keys %h)
> {
> $a =~ s/\b$_\b/$h{$_}/g;
> }
>
> I want to replace matches in string to hash key with hash value. I am
> replacing acronyms with phrases where acronym is hash key. Is there a
> better or different way?
Well, here's one that's different, though not necessarily better (in
fact, quite likely worse)
$a=~ s/\b(\B+)\b/$h{$1} or $1/ge;
Rather than searching the string for each hash key, this one would
search each word in the string to determine if it is in the hash (more
correctly stated: if it has a true value in the hash). If so, replace
it with the hash value, if not, leave it as is.
Benchmarking is left as an excercise to the OP.
One comment, however: Be careful about the use of \b. While \b does
mean 'word boundary', it means Perl's definition of a 'word', which is:
[0-9a-zA-Z_]+ That means that "don't" is two words: "don" and "t".
This may or may not be what you actually want.
Paul Lalli