Link to home
Start Free TrialLog in
Avatar of rgbcof
rgbcof

asked on

perl, advanced text substitution

%hash = (YEAR_95 => '1st daughter', YEAR_97 => '2nd daughter', YEAR_99 => '1st son');
$string = "First YEAR_95, then YEAR_97,  then YEAR_99";

How to substitute pattern YEAR_nn in the string with the hash value?  
So that the result is:  "First 1st daughter, then 2nd daughter,  then 1st son";
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgbcof
rgbcof

ASKER

That works.  Awesome.
print "First $hash{'YEAR_95'}, then $hash{'YEAR_97'}, then $hash{'YEAR_99'}\n";
$string=~/(YEAR_\d\d)/$hash{$1}/g;
$string=~s/(YEAR_\d\d)/$hash{$1}/g;