Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Returning regexp pattern that was used to match

by Ven'Tatsu (Deacon)
on May 03, 2004 at 14:58 UTC ( #350032=note: print w/ replies, xml ) Need Help??


in reply to Returning regexp pattern that was used to match

First off a few comments not related to your question:
If your using any other regexs in your program avoid $& wrap your test in a () instead. $& will cause all regexs to run slower through your whole program.
Don't use map in a void context, it throws away the output. Try for (keys(${$args})) {$string .= $_ . "|"} I would usualy consider join even better but it would not work for my idea bellow.

As for you question you could try (?{ CODE }) to set a variable to the matched code, maybe something like this:

our $matched_key; #global to hold the key that matched, off the top of + my head I don't think a 'my' variable would work. for (keys($$args)) { $string .= "$_(?{$matched_key = $_})|"; }


Comment on Re: Returning regexp pattern that was used to match
Select or Download Code
Don't cargo cult "advice"
by Abigail-II (Bishop) on May 03, 2004 at 15:04 UTC
    Don't use map in a void context, it throws away the output.
    What output does it throw away? What about using assignment or print in void context? Not using print in void context because some "output" is thrown away?

    Abigail

      Each $string .= ... returns the new contents of $string. map builds an array with the values of last statment execuded in it's block. This cost both time and memory.
      foreach does not keep the values it's block returns so it will run faster than map.
      The OP's code did not use any of the results a concatination assignment (other than the actual concationation assignment side effect) so keeping the results around is a waste.
      In the case of assignment or print your usualy not interested in the direct result, only the side effect. So storing the result is a waste of time and memory.

      See What's wrong with using grep or map in a void context?

      This was changed in 5.8.1 (map only, grep should still be avoided in void context), but as there are plenty of installs running perls prior to that it's still a bad idea.
        5.8.1 fixed a long standing bug. Celebrate that, and upgrade.

        Abigail

Re: Re: Returning regexp pattern that was used to match
by crabbdean (Pilgrim) on May 03, 2004 at 15:18 UTC
      (?{ CODE }) will execute any perl code when the regex engine tries to match it. So for example:
      my $string = "Bar"; $sting =~ /Foo(?{ print "Found a Foo\n" })|Bar(?{ print "Found a Bar\n +" })|Baz(?{ print "Found a Baz\n" })/;
      Should print out 'Found a Bar'.

      It's documented in the perlre man page under 'Extended Patterns'.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-07-29 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls