in reply to Regular Expression Loop Hell

From your description, it sounds like you want something like this (mildly tested):

#!/usr/bin/perl use strict; my @animals = ( "bear<1>\n", "camel<0> <2>\n", "<2>horse\n", "duck<3>\ +n" ); my %changemap = ( "<0>" => 'A', "<1>" => 'B', "<2>" => 'C', "<3>" => 'D', ); # build a single regular expression to match all of the # items we're changing from my $from = join '|', keys %changemap; for (@animals) { s/($from)/$changemap{$1}/g; } print @animals; __END__
update: removed line in the code that compiled the RE. I don't think it adds anything in the way of understanding.

Replies are listed 'Best First'.
Re^2: Regular Expression Loop Hell
by Roy Johnson (Monsignor) on Mar 07, 2006 at 22:32 UTC
    As a general precaution, when joining a bunch of terms into a regex alternation series, it's best to sort them by length, longest first, so that, for example, foobar will not be pre-empted by foo. It doesn't make any difference in your example, of course.

    Caution: Contents may have been coded under pressure.