Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Returning _which_ pattern matched...?

by lestrrat (Deacon)
on Aug 03, 2001 at 18:53 UTC ( #101996=note: print w/replies, xml ) Need Help??


in reply to Returning _which_ pattern matched...?

If you happen to have a lot of patterns to match, I like the "build a sub on the fly" approach

my @patterns = qw/ foo bar baz /; ## if you need to return the pattern itself, return '$_' instead my $match_n_return = join( "\n", map{ "return \$1 if \$_[0] =~ /$_/o;" } @patterns ); my $sub = eval qq|sub { $match_n_return return undef }|; if( $@ ) { ## some syntax error in the eval string die $@ } my $matched = $sub->( "string you want to match" );

The only problem here is, you don't get a fine granularity of control for each match. But I like the fact that this can be used over and over, and that since all the regexes are pre-compiled, you save some time for repeated use

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2023-03-28 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (67 votes). Check out past polls.

    Notices?