Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Finding out which of a list of patterns matched

by Not_a_Number (Prior)
on Jan 22, 2006 at 15:55 UTC ( [id://524794]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Finding out which of a list of patterns matched
in thread Finding out which of a list of patterns matched

I still like my method below better <snip>

I don't. The OP specifically states "I'll potentially have quite a lot of patterns to test". What if "quite a lot" means, say, 50, or 500, or 5,000? Your dispatch table could become unmanageably long...

  • Comment on Re^3: Finding out which of a list of patterns matched

Replies are listed 'Best First'.
Re^4: Finding out which of a list of patterns matched
by tirwhan (Abbot) on Jan 22, 2006 at 16:14 UTC

    By "my method" I meant using capturing parentheses and $1 to retrieve the match, instead of $#-. The dispatch table is just the next step and not tied to the method of finding the value to act on.

    But regardless of that, what's the problem with a long dispatch table? You don't actually need to store the actions as anonymous subs of course, if you have several values which correspond to the same action you just use references to subs which you define elsewhere. This is certainly more manageable than endless if-elsif blocks. Sure, it's no silver bullet (e.g. if you only have three actions which each map to 1000 possible keywords you probably wouldn't go that way), but without knowing more about the OPs requirements it's impossible to tell how appropriate the solution is.

    Maybe I'm not understanding your objection correctly though, would you care to give a solution that's more manageable for 5000 values?

    Update: here's an example of how you could set up a dispatch table with lots of values matching to less (and potentially longer) subroutines:

    my @v1=qw(horse hearse house hose); my @v2=qw(fax fix fox flux); my @v3=qw(orange lemon melon plum apple banana rama); my $regex = join("|",@v1,@v2,@v3); sub one { print "Begins with an h\n"; } sub two { print "Oh those Xes\n"; } sub three { print "Yummy!\n"; } my %table; @table{@v1,@v2,@v3}=((\&one)x scalar(@v1), (\&two)x scalar(@v2), (\&three)x scalar(@v3)); if (my ($match) = $input =~ m/($regex)/) { &{$table{$match}}; }

    Seems manageable to me, even if you imagine this expands by a considerable amount.


    There are ten types of people: those that understand binary and those that don't.
      By "my method" I meant using capturing parentheses and $1 to retrieve the match, instead of $#-.

      Sorry for any misunderstanding. My quibble was with your use of a dispatch table, not with the rest of your code.

      But regardless of that, what's the problem with a long dispatch table?

      I have no issue with the use of dispatch tables per se, long or otherwise. What I meant to point out was that there is absolutely no need for a dispatch table in the context of the OP.

      dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found