http://qs321.pair.com?node_id=665218

paulehr has asked for the wisdom of the Perl Monks concerning the following question:

Greetings fellow monks, this poor novice has been struggling away at a simple problem for quite some time now. I am trying to test to see if the value of a variable is one of two values. If it is neither print a message indicating there was an error.

#!/usr/bin/perl -w use strict; my $var = "yes"; if ($var eq ("yes" or "no")) { print "You choose $var\n"; }else{ print "you chose something other then yes or no\n" }

Now if I change $var to something like "what", I get "you chose something other then yes or no". If I change $var to "yes", I get "you choose yes". If I change $var to "no", I would expect to get "you choose no". However I am getting "You chose something other then yes or no". Am I missing something here?

Thanks for the help in advance!

Replies are listed 'Best First'.
Re: Need help testing to see if a variable is one value or the other
by moritz (Cardinal) on Jan 30, 2008 at 21:16 UTC
    if ($var eq "yes" or $var eq "no"){ ... } will do the job.

    The way you write it is planned for Perl 6:

    if $var eq any <yes no> { ... }

      thanks guys!

      I knew this was probably going to be something easy. I do not know why I was having a hard time with it.

Re: Need help testing to see if a variable is one value or the other
by ikegami (Patriarch) on Jan 30, 2008 at 21:27 UTC

    Here's the reason why:
    "yes" or "no"
    evaluates to "yes", so
    $var eq ("yes" or "no")
    is the same as
    $var eq "yes"

Re: Need help testing to see if a variable is one value or the other
by Fletch (Bishop) on Jan 30, 2008 at 21:18 UTC

    Alternately (heh) use a regex.

    if( $var =~ m/\A ( yes | no ) \z/x ) { print "you chose $1\n"; } else { print "BAD USER, NO DONUT!\n"; }

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

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

    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
Re: Need help testing to see if a variable is one value or the other
by traveler (Parson) on Jan 30, 2008 at 22:36 UTC
    Or, if GrandFather's solution is too elegant and simple, you can use a module. (It is way overkill here, though!)
    use strict; use Set::Scalar; my $s = Set::Scalar->new( qw/yes no/ ); my $var = "no"; if ( $s->has($var)){ print "found $var\n"; } else{ print "never heard of $var\n"; }
Re: Need help testing to see if a variable is one value or the other
by Skeeve (Parson) on Jan 31, 2008 at 07:22 UTC

    TMTOWTDI

    #!/usr/bin/perl -w use strict; my %allowed_reply= ( yes => 1, no => 1, ); my $var = "yes"; if ( $allowed_reply{$var} ) { print "You choose $var\n"; }else{ print "you chose something other then yes or no\n" }

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Need help testing to see if a variable is one value or the other
by jozxyqk (Initiate) on Jan 31, 2008 at 15:46 UTC
    In Perl 5.10 you could use "given": (This is a guess from the documentation; I haven't actually played with 5.10 myself)
    use feature qw(say switch); given($var) { when (['yes','no']) { say "You chose $var"; } default { say "you chose something other than yes or no"; } }
Re: Need help testing to see if a variable is one value or the other (~~ ARRAY)
by lodin (Hermit) on Feb 01, 2008 at 11:48 UTC

    This is pretty much the same as jozxyqk's reply, except it doesn't pull in given/when. The only modification to your code is to replace eq ("yes" or "no") with ~~ [ "yes", "no" ].

    #!/usr/bin/perl -w use strict; my $var = "yes"; if ($var ~~ [ "yes", "no" ]) { print "You choose $var\n"; }else{ print "you chose something other then yes or no\n" }

    lodin