Re: if or switch?
by Tanktalus (Canon) on Jul 10, 2008 at 22:21 UTC
|
I'd go with a lookup-table. Since you can look up directly in a hash table, a list will have to do...
my @keys = (
[ "key1" => sub { handle_key1 } ], # inline anony sub
[ "key2" => \&handle_key2 ], # use a named function
[ "key3" => sub { ... } ],
# ...
);
use List::Util qw(first);
# find the entry
my $data = first { index($mystring, $_->[0]) >= 0 } @keys;
# call the code
$data->[1]->();
| [reply] [d/l] |
Re: if or switch?
by FunkyMonk (Bishop) on Jul 10, 2008 at 22:24 UTC
|
Use a dispatch table. Something like...
my $mystring = "hello, I'm key2";
my %dispatcher = (
key1 => sub { print "1" },
key2 => sub { print "2" },
#...
);
#...
for my $key ( keys %dispatcher ) {
$dispatcher{$key}->(), last
if index($mystring, $key) >= 0;
}
| [reply] [d/l] |
|
|
Using a hash here means you'll get the keys back in a random order. Which may be ok, but can fall down in two scenarios.
First is if the keys can be subsets. e.g., if one is "key1" and another is "key10". You'll want to make sure you get them in order from longest to shortest. Easy enough to do:
for my $key ( reverse sort {length $a <=> length $b} ) {
This should be pretty fast. Can take a while for big lists, but then so can the index that we're about to run, so worrying about this O(N ln N) operation seems premature.
The second failure is if you know that certain strings are more likely than others. So you can manually order the keys based on likelihood. This can really speed up the searches, too, if you're constantly short-circuiting things.
This is why I opted, above, for a list of array refs instead of a hash. | [reply] [d/l] |
Re: if or switch?
by Skeeve (Parson) on Jul 10, 2008 at 22:27 UTC
|
While I agree that my code isn't the best, I tend to write switches like this (which I learned back in 1995, using perl 4)
SWITCH: for ($mystring) {
/key1/ && do {
…
last SWITCH;
};
/key2/ && do {
…
last SWITCH;
};
/key3/ && do {
…
last SWITCH;
};
# default
…
last SWITCH;
}
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
Re: if or switch?
by pc88mxer (Vicar) on Jul 10, 2008 at 22:52 UTC
|
What are your actions for each of the three cases?
If they are very different, then a dispatch table or a Perl6 switch statement is about the most efficient.
If the code for all three cases is very similar, then perhaps this can be made to work for you:
if (m/key(\d)/) {
# $1 contains the digit following "key"
print "Found key followed by a $1\n"
$count{$1}++;
print "Total times this key was seen: ", $count{$1}, "\n";
...
}
| [reply] [d/l] |
Re: if or switch?
by NetWallah (Canon) on Jul 10, 2008 at 22:44 UTC
|
use Switch 'Perl6';
given ($mystring) {
when /key1/ { handle_Key1(); }
when /key2/ { handle_Key2(); }
when /key3/ { handle_Key3(); }
default { handle anything else; }
}
Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax
| [reply] [d/l] |
|
|
| [reply] |
|
|
chromatic - are you red flagging (or yellow-flagging) the use of the switch statement ?
If so, could you specify which version you had bad experience with ?
- Switch Module? V5 or V6 option ?
- Perl6 (Rakudo, or pugs)?
- Perl 5.10 "feature" module ?
- Perl5 FAQ that fakes out "case" ?
Was this an esoteric case ?
Your response could help the community either toward reproducing and fixing bugs, or by directing programming practices based on your recommendation.
Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax
| [reply] |
|
|
Re: if or switch?
by gw1500se (Beadle) on Jul 10, 2008 at 22:48 UTC
|
Thanks for all the suggestions. I did not glean the reg expr construct from the switch documentation and clear examples are rare. The other 2 methods are interesting as well. I would not have though of those. Now I have to think about them and make a choice and I hate choosing. :-) | [reply] |
Re: if or switch?
by Arunbear (Prior) on Jul 11, 2008 at 16:10 UTC
|
With Perl 5.10, you can do this:
use strict;
use feature qw(switch say);
use warnings;
foreach my $str (qw/fookey1 barkey2 bazkey3/) {
given ($str) {
when (/key1/) { say 'matched key1'; }
when (/key2/) { say 'matched key2'; }
when (/key3/) { say 'matched key3'; }
}
}
See the docs for Switch statements.
| [reply] [d/l] |