Gossamer Forum
Home : General : Perl Programming :

Shorter Way for search and replace

Quote Reply
Shorter Way for search and replace
I have to replace months alphabets to months nos. in a string.

$date =~ s/Mar/03/g;
$date =~ s/Apr/04/g;
$date =~ s/May/05/g;


I don't want to repeat the same for tweleve months.

Is there any shorter way to do it?

like e.g. $date =~ s/Mar,Apr,May/03,04,05/g;

Thanks,

Zeshan.
Quote Reply
Re: [zeshan] Shorter Way for search and replace In reply to
Well you *could* do something like:

Code:
my %map = (
Jan => '01',
Feb => '02',
Mar => '03',
...
);

$date =~ s/(Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sept|Oct|Nov|Dec)/$map{$1}/g;