in reply to Storing Regular Expressions as Strings in Array??

Hmm. Well qr// only stores the pattern not the replacement.

You could try setting up a hash or list of closures with the appropriate regex in them:

my @subs; push @subs,sub{local $_=shift; s/foo/bar/g; $_}; push @subs,sub{local $_=shift; s/baz/zaz/g; $_}; my $string="foo baz"; $_->($string) foreach @subs; print $string;
Something like that might do the trick...

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

Replies are listed 'Best First'.
Code Correction
by arunhorne (Pilgrim) on May 02, 2002 at 17:53 UTC

    I had to make the following change to the above snippet to be able to make it work as intended... i.e. the $string ends up as "bar zaz".

    my @subs; push @subs,sub{local $_=shift; s/foo/bar/g; $_}; push @subs,sub{local $_=shift; s/baz/zaz/g; $_}; my $string="foo baz"; $string = $_->($string) foreach @subs; # *** CHANGED *** # print $string;

    Thanks for the code though, it was very helpful

      Doh.

      Of course. The way I did it would work only if the subs were written as

      sub{$_[0]=~s/foo/bar/g; $_[0]}
      Sorry about the confusion...

      Glad it helped out though. :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.