Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^2: Precompiling substitution regex

by FreakyGreenLeaky (Sexton)
on Jun 17, 2011 at 11:57 UTC ( #910131=note: print w/replies, xml ) Need Help??

in reply to Re: Precompiling substitution regex
in thread Precompiling substitution regex

Thanks moritz, that will work. Is there any way of achieving that without having the substitutions in a separate array?

Replies are listed 'Best First'.
Re^3: Precompiling substitution regex
by moritz (Cardinal) on Jun 17, 2011 at 12:04 UTC
    Well, you could of course store an array of closures that each does a substitution, and call those in turn - but it might slow down things again. Or you could store regexes and substitutions in the same array, but distinguished by different indexes (even/odd or first half/second half).

    But I don't think you can easily store a precompiled whole substitution in a single scalar.

      ... store regexes and substitutions in the same array, but distinguished by different indexes ...

      An example of what I think moritz is referring to (and the way I think I would approach the problem):

      >perl -wMstrict -le "my @subs = ( [ qr{ (?i) foo | bar }xms => 'quux' ], [ qr{ fee | fie | foe }xms => 'fum' ], ); ;; my $string = 'foo fee BAR foe fOo fie FOE'; for my $ar_sub (@subs) { my ($search, $replace) = @$ar_sub; $string =~ s{$search}{$replace}g; } print qq{'$string'}; " 'quux fum quux fum quux fum FOE'

      Note that regex modifiers like /g cannot be associated with each individual regex using this approach; something a little more fancy is needed for that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://910131]
jedikaiti is glad to see the conditioning persists...
[jedikaiti]: And yes, I have saved down a copy and added it to my walpaper rotation
[zentara]: jedikaiti my condition has a condition :-)
[jedikaiti]: :-D
[Your Mother]: ♫ You say apod, I say Perl POD ... let's call the whole thing off ♬

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2017-08-18 15:42 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (304 votes). Check out past polls.