Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Need help testing to see if a variable is one value or the other

by GrandFather (Saint)
on Jan 30, 2008 at 21:40 UTC ( #665227=note: print w/replies, xml ) Need Help??


in reply to Need help testing to see if a variable is one value or the other

You can generalize the code somewhat:

#!/usr/bin/perl use strict; use warnings; my @options = qw(yes no); my $var = 'Yes'; my $isOption = join '|', @options; if ($var =~ /^($isOption)$/i) { print "Matched '$1'\n"; } else { print "Want one of: @options\n"; }

Prints:

Matched 'Yes'

Not that the match is now case insensitive (the /i on the regex) and it is now trivial to add new options to be matched.


Perl is environmentally friendly - it saves trees

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://665227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-01-25 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (66 votes). Check out past polls.

    Notices?