in reply to Re^2: problem with substitute in regexp
in thread problem with substitute in regexp

roboticus
#! /usr/bin/perl -w use strict; my %replace = ('111'=>"bar", '222'=>"world", '333'=>"perl"); my $str = "f111,f222,f333"; $str =~ s/f([0-9]+)/$replace{$1}/g; print "$str\n";

Replies are listed 'Best First'.
Re^4: problem with substitute in regexp
by roboticus (Chancellor) on Nov 10, 2011 at 15:53 UTC

    ansh batra:

    For the third point, I was just meaning that you don't need the parenthesis. But I was totally wrong (as AnomalousMonk pointed out to me), as I missed that you *are* using the capture group as the key to the hash for your replacement. As you can see, we *all* make mistakes! ;^)

    Your latest version looks good.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.