Yea there are a lot of values and if I go back and look at my code I do have
eq instead of the =. I still had the multiple parens though. I was thinking
that the comparison may hold true when it crosses the "or" statement without
the parenthesis.  I will give the hash a try and let you know if I have any
problems.  Thanks again for all of the help.

Doug
  

 -----Original Message-----
From:   Jeff Pinyan [mailto:[EMAIL PROTECTED]] 
Sent:   Thursday, May 17, 2001 10:29 AM
To:     Doug Johnson
Cc:     [EMAIL PROTECTED]
Subject:        Re: Compare statement.

On May 17, Doug Johnson said:

>my $x = "return";
>
>if (($x = "a") || ($x = "test" ) || ($x = "return" ) || ($x = "x-retun"))
>   {
>   print("bunch of foo....);
>   }

First, that's NOT the code you have.  If it IS, it's broken.

  if ($x eq 'a' or $x eq 'test' or $x eq 'return' or $x eq 'x-return') {
    # ...
  }

(Yes, you can use parens if you want, and || instead of 'or', but the
point is, using = is ASSIGNING to $x.  And using == on strings is a
no-no.  You need to use 'eq'.)

You might think it's overkill, but I might use a hash if there are a lot
of values.

  my %valid_x;
  @valid_x{'a', 'test', 'return', 'x-return'} = ();

  if (exists $valid_x{$x}) {
    # ...
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
*** I need a publisher for my book "Learning Perl Regular Expressions" ***

Reply via email to