in reply to Re^3: Finding out which of a list of patterns matched
in thread Finding out which of a list of patterns matched
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Finding out which of a list of patterns matched
by Not_a_Number (Prior) on Jan 22, 2006 at 17:55 UTC |