On Aug 27, 2007, at 10:59 AM, Petra Vide Ogrin wrote:

Hi all,

I have a hash and some prose text and want my perl to identify the keys of the hash in this text and replace them with the corresponding values of
the keys.

I tried the following

foreach (keys %expan) {
  if ($sbl =~ m/$_/g) {
    $sbl =~ s/$_/$expan{$_}/g;

No need for the previous check, if it can't perform the substitution it won't.

That assumes no key is part of another key's value, if it was the case you could end up doing double/triple/... substitutions. I would switch to a one-shot (untested) substitution:

  my $re = join '|', map quotemeta, keys %expan;
  $sbl =~ s/\b($re)\b/$expan{$1}/g;

I don't know why are you applying that substitution, but smells like a use case for some templating system like Text::Template. If it is please consider basing the script on it.

-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to