Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Comparing against multiple values

by shotgunefx (Parson)
on Mar 18, 2002 at 20:02 UTC ( #152578=note: print w/replies, xml ) Need Help??


in reply to Comparing against multiple values

If the result is to call some sub then I would just use a hash but sometimes I use a little snippet like so.
#!/usr/bin/perl sub matches(&;@) { my $coderef = shift ; my @data = @_; for (my $i=0; $i <= $#data; $i++ ){ $_ = $data[$i]; if (&$coderef){ return wantarray ? ($_ ,($i ) ) : $_; } } return; } my @actions = qw(get fetch store info print); if (my ($v,$i) = matches { $action eq $_ } @actions ){ print "Found $v at $i\n"; } # Or if (my $v = matches { $action eq $_ } @actions ){ print "Found $v $i\n"; } # Or die "Damn! Invalid action $action" unless matches{$action eq $_ } @act +ions;


-Lee

"To be civilized is to deny one's nature."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://152578]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2018-02-23 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (310 votes). Check out past polls.

    Notices?