On May 22, Paul said:

>I know that in many C compilers, the a ? b : c construct with the
>ternary ?: operator si not stable after the second or third nesting,
>but I've never seen that sort of problem in tests I've run in Perl.

The only to watch out for is precendence:

  $a ? $b = $c : $b = $d;

is like

  ($a ? $b = $c : $b) = $d;

which always sets $b to $d.

> sub rate ($) {
>    $_[0] eq 'A' ? .03 :
>    $_[0] eq 'B' ? .05 :
>    $_[0] eq 'C' ? .06 :  
>                   .08;   # the default
> }

That's fine, but I prefer to use hashes for that sort of thing.

  {
    my %rates = qw(
      A .03
      B .05
      C .06
    );
    sub rate {
      exists $rates{$_[0]} ? $rates{$_[0]} : .08
    }
  }

-- 
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's Regular Expressions" **

Reply via email to