
BillKSmith
Veteran
Feb 13, 2013, 10:04 AM
Post #7 of 9
(316 views)
|
It is not necessary to split the text. Use the /e option to evaluate the hash in the replacement field of the substitution.
use strict; use warnings; my %number = ( one => 1, two => 2, three => 3, ); while (my $line = <DATA>) { $line =~ s/(:?one|two|three)/$number{lc $1}/egi; print $line, "\n"; } __DATA__ John has three apples, two cats and one girlfriend Ouput:
John has 3 apples, 2 cats and 1 girlfriend EDIT: Minor correction to make code case insensitive. Good Luck, Bill
(This post was edited by BillKSmith on Feb 13, 2013, 10:18 AM)
|