in reply to Trinary or If'n'Else?

On a related note, could anyone explain the difference between:
a ? b : c
a && b || c
(besides the fact that the hook operator can be assigned to).

Replies are listed 'Best First'.
Re: Re: Trinary or If'n'Else?
by Maclir (Curate) on Dec 22, 2000 at 08:52 UTC
    The main thing about the trinary operator is that, quoting your case above, "a" is a logical expression; where as b and c may be any expressions that return a result compatible with what you are assigning the expression to (or the other way around, if you are assinging to the operator). While most of the examples mentioned are just simeple assignments ($fred = (some_condition)? $bar : $foo), the statement is much more powerful than that.
      I'm not sure I understand. As far as I can see, ?: and &&|| do the same thing:
      print 1 ? 'true' : 'false'; # True print 1 && 'true' || 'false'; # True print 0 ? 'true' : 'false'; # False print 0 && 'true' || 'false'; # False
      In both a ? b : c and a && b || c, a is a boolean expression. What can ?: do that &&|| cannot?

        I quickly came up with at least two:

        DB<1> x 0 && qw(t r u e) || qw(f a l s e) 0 'f' 1 'a' 2 'l' 3 's' 4 'e' DB<2> x 1 && qw(t r u e) || qw(f a l s e) 0 'e' # Oops! DB<3> x 1 && 2 || "two" 0 2 DB<4> x 1 && 0 || "two" 0 'two' # Oops!
        So "b" can't be a false value and can't be a list.

                - tye (but my friends call me "Tye")