Next | Tricks of the Wizards | 211 |
A better way:
$pat = join '|', map "($_)", @state_pat;
$pat now looks like:
(Alaska)|(Alabama)|...|(Wyo(?:\.|ming)?)
Now use:
my @matchlist; if (@matchlist = ($input =~ /$pat/o)) { my $i = 0; ++$i until defined $matchlist[$i]; return $state_abbr[$i]; } else { return; }
Caution: Important to use (?:...) instead of (...) in subpatterns.
Next | Copyright © 2003 M. J. Dominus |