Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re^4: Finding out which of a list of patterns matched by tirwhan
in thread Finding out which of a list of patterns matched by lemnisca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-20 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found