in reply to Logical Operators

if ($value1 == 30 and $value2 > 0 and $string1 eq "winner") { print "winner!\n"; }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Logical Operators
by Zombie (Acolyte) on May 23, 2001 at 21:08 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.