in reply to Regular Expression Loop Hell

Try This
my @animals = ( "bear<1>\n", "camel<0> <2>\n", "<2>horse\n", "duck<3>\ +n" ); my @changeTo = ( 'A', 'B', 'C', 'D' ); my @toFind = ( '<0>', '<1>', '<2>', '<3>' ); @hs{@toFind}= (@changeTo); foreach $anm (@animals) { $anm =~ s/$_/$hs{$_}/g, for(keys %hs); print "$anm"; }

Replies are listed 'Best First'.
Re^2: Regular Expression Loop Hell
by pjnet (Novice) on Mar 08, 2006 at 17:31 UTC

    Run this code and you'll see exactly why that code is not helpful. It's not about the regex, it's about the double loop.

    #!/usr/bin/perl -w my @animals = ( "bear<1>\n", "camel<0> <2>\n", "<2>horse\n", "duck<3>\ +n" ); my @changeTo = ( 'A', 'B', 'C', 'D' ); my @toFind = ( '<0>', '<1>', '<2>', '<3>' ); @hs{@toFind}= (@changeTo); $i = 0; while($i < 5) { foreach $anm (@animals) { $anm =~ s/$_/$hs{$_}/g, for(keys %hs); print "$anm"; } shift(@changeTo); push(@changeTo, int(rand(10000))); @hs{@toFind} = (@changeTo); print "\n"; print "@changeTo"; $i++; print "\n\n"; }