Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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 meditating upon the Monastery: (4)
As of 2018-06-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (116 votes). Check out past polls.

    Notices?