Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: Tokenizing and qr// <=> /g interplay

by ff (Hermit)
on Apr 25, 2005 at 11:59 UTC ( [id://451144]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Tokenizing and qr// <=> /g interplay
in thread Tokenizing and qr// <=> /g interplay

Hi, I was just refactoring some code and saw a possible opportunity to use this advice. But. Instead of having multiple strings in @strings to process, I leave all the lines from my file joined together as one giant string with embedded \n chars. From this angle, since I only have to use each regex once across all the strings via them being 'joined' into one string, I won't benefit from qr.

my ( $crummy, $good ); foreach my $crummy_good_ar ( @corrections_to_make ) { ( $crummy, $good ) = @$crummy_good_ar; $file_in_string_form =~ s/\b(\Q$crummy\E)\b/$good/ig; }
However, as you can see (?) from the example above, I have lots of crummy/good switchouts to do, and is my plodding approach above the best that can be expected?

P.S. Can you clarify/update what you meant by:
the benefit of qr// objects is lost if there is additional text in the pattern match

I think you are saying that a precompiled/qr regex used in a follow-on regex will have to be recompiled if you snap additional text on to the qr'd variable, because the overall text of the new regex will be different. Although at least one would still have the benefit of 'concentrated regex logic' within the qr'd variable?

Replies are listed 'Best First'.
Re^5: Tokenizing and qr// <=> /g interplay
by japhy (Canon) on Apr 25, 2005 at 12:36 UTC
    I can answers your first question by answering your second one. If you made all the first elements of your array references Regexp objects:
    $_->[0] = qr/\b(\Q$_->[0]\E)b/ for @corrections_to_make;
    then you could do your loop as
    for my $crummy_good_ar (@corrections_to_make) { my ($crummy, $good) = @$crummy_good_ar; $file_in_string_form =~ s/$crummy/$good/ig; }
    This way, even if you end up looping over THAT code, you'd still be dealing with already-compiled regexes. As soon as you put additional text into a regex with qr// in it:
    my $rx = qr/abc/; if ($str =~ /^($rx)$/) { ... }
    Perl has to do the "compare physical regex forms" test. Only if the qr// object is all alone will it have the entire benefits it was made for.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Okay, so an array of [ qr, $good ] combos could help me if my example was something like:

      for my $file_in_string_form ( @all_files ) { for my $crummy_good_ar (@corrections_to_make) { my ($crummy, $good) = @$crummy_good_ar; $file_in_string_form =~ s/$crummy/$good/ig; } }
      And in the meantime, if I have no additional files to process, I might as well compile the regexen as needed?

      Thanks! I think I understand now. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://451144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-06-15 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.