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