[Israel.pm] regex substitue with lookup table

Offer Kaye oferk at oren.co.il
Sun Mar 28 02:02:54 PST 2004


> Hi,
>
> I have a code that looks like this:
>
> if (/someText (.+?) someMoreText (\d+)/)
> {
>       if (exists $hash{$2})
>       {
>             my $newStr=$hash{$2};
>             s/someText $newStr someMoreText/;
>       }
> }
>
> I know There Is Another Way To Do It, and even a shorter one - how ?
>
> Thanks
>
> Yossi
>

> Sorry,
> It should be:
> s/someText (.+?) someMoreText/someText $newStr someMoreText/;
>


Ah, I see now. Well, I guess one shorter version of the above code is:
s/someText (.+?) someMoreText (\d+)/(exists $hash{$2})?"someText $hash{$2}
someMoreText $2":"someText $1 someMoreText $2"/e;

But frankly in this case I personally would go for the longer way- it's
simply more readable.

----------------------------------
Offer Kaye



More information about the Perl mailing list