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

Re: Double interpolation of captured substrings

by BrowserUk (Pope)
on Nov 17, 2004 at 10:26 UTC ( #408359=note: print w/ replies, xml ) Need Help??


in reply to Double interpolation of captured substrings

You can do away with the hash completely by passing the order as a list rather than a string.

#! perl -slw use strict; sub munge { my( $str, $regex ) = ( shift, shift ); my @matches = ( undef, $str =~ $regex ); return join'', @matches[ @_ ]; } print munge( 'one_two_three', '^([^_]+)_([^_]+)_([^_]+)$', 2, 1, 3 ); __END__ [10:24:12.59] P:\test>junk twoonethree

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon


Comment on Re: Double interpolation of captured substrings
Download Code
Re^2: Double interpolation of captured substrings
by Fletch (Chancellor) on Nov 17, 2004 at 16:59 UTC

    You could also allow for referencing the whole match as 0 with:

    $str =~ $regex; my @matches = map substr( $str, $-[$_], $+[$_] - $-[$_] ), 0..$#-;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-12-22 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (112 votes), past polls