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

if ($value1 == 30) { if ($value2 > 0 ) { if ($string1 eq "winner" ) { print ("You are a WINNER!\n"); } } }
This code is tiresome to write, and difficult to read too. How do I write codes to test multiple conditions more conveniently?

Replies are listed 'Best First'.
Re: Logical Operators
by merlyn (Sage) on May 23, 2001 at 20:38 UTC

      Thanks. This is only part of my code. There are actually 25 conditions. If I use the "and" operator , is there a limit on how large my expression can be?

        No, but if you have 25 conditions, there's almost certainly some different way of doing it. For further help, describe how those conditions are determined, and how the data to which it's being compared is determined. I'm sure there's a pony in there somewhere.

        -- Randal L. Schwartz, Perl hacker

        If you're trying to test 25 different variables, I'm at a loss. If you can somehow get all the conditions into a few variables, you can construct a switch-like statement. While there's no switch construct in perl there are many ways to construct one. My favorite is to use a single-iteration "for" loop, straight from the 2nd Edition Camel:
        for( $value ) { /matchthis/ and do{ something(); last;}; ($_ > 42) and do{ something_else(); last;}; default(); }
        so long as the first term in an entry in the for loop returns a boolean value, you don't need to limit yourself to strict equality operations, which is a real bonus in my eyes over the conventional switch statement found in other languages.

        update: If you can't get it down to one variable, but can get it down to two or three, you can always put another one of these pseudo-switches inside one of the do blocks.

Re: Logical Operators
by princepawn (Parson) on May 23, 2001 at 21:51 UTC
    you might find some help in Switch

      This is actually to check the answers to a 25 questions quiz. If someone get all the answers correct, then a prize is given. Is there a better way to write the codes?

        Certainly.

        Each time an answer is given, see if it's right, then increment a counter. At the end, you'll have the number of right answers. The code below reads very much like that sentence:

        # The code has already asked a question at this point # ("What is the answer to Life, the Universe, and Everything"). # The user has put in his/her answer. # The program has stored it in a scalar, $answer. if ($answer == 42) { $rightanswers++; } # Put that sort of logic in your question/answer loop, # then you can end with something like this: if ($rightanswers == 25) { print "You got all of the questions right! You win!\n"; } elsif ($rightanswers > 20) { print "You got almost all of the questions right. Not bad!\n"; } elsif ($rightanswers > 10) { print "You could've done better...\n"; } elsif ($rightanswers > 1) { print "You did quite poorly...\n"; } else { print "Were you even reading the questions...?\n"; }