Next Tricks of the Wizards 211

Matching Many Patterns at Once

        $pat = join '|', map "($_)", @state_pat;
        (Alaska)|(Alabama)|...|(Wyo(?:\.|ming)?)

        my @matchlist;        
        if (@matchlist = ($input =~ /$pat/o)) {
          my $i = 0;
          ++$i until defined $matchlist[$i];
          return $state_abbr[$i];
        } else {
          return;
        }


Next Copyright © 2003 M. J. Dominus