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

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://408359]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2018-06-25 20:23 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.