Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: variable set to 0 ? 0 : 1

by nmerriweather (Friar)
on Sep 08, 2002 at 01:30 UTC ( [id://195952]=note: print w/replies, xml ) Need Help??


in reply to variable set to 0 ? 0 : 1

i've never used the trinary operator before -- most of my subs w/returns give back a #, not a true/false

i understand its use for return statements, but am a little unclear about other ways to use it (perhaps changing 1/0 to 'true'/'false' or some other manipulation of a variable?)

i was hoping some people would be willing to share some other examples/samples/instances using this operator is good/appropriate for

Replies are listed 'Best First'.
Re: Re: variable set to 0 ? 0 : 1
by BrowserUk (Patriarch) on Sep 08, 2002 at 02:47 UTC

    One interesting use that is (as far as I know) unique to Perl, is using the trinary as an Lvalue. That is on the left hand-side of an assignment as in this (rather silly) example.

    #! perl -sw use strict; my (@aboveC, @cOrLess); while (<DATA>) { # $1 := name, $2 := grade m/^(\w+)\s+([a-f][+-]?)$/i; # if the grade is one of these 0<= index('A+ A A- B+ B B- C+', uc($2), 0 ) # add name here if not add name here ? $aboveC[@aboveC] : $cOrLess[@cOrLess] = $1; } print "Above C students: @aboveC\n"; print "C or Less students: @cOrLess\n"; __DATA__ homer f bart d- marge c+ lisa a

    which gives

    C:\test>195952 Above C students: marge lisa C or Less students: homer bart C:\test>

    Well It's better than the Abottoire, but Yorkshire!
      I have to admit being excited about this construct the first time I saw it, though I don't know if I've ever really used it....
      push(@{ $whichone ? \@arr1 : \@arr2 }, $element);

      -Blake

        Oh, I use that quite frequently: push @{ -d "$dir/$_" ? \@dir : \@file }, $_ for readdir DIR; It's pretty much the only use I have for it, but it's a perfect match for that.

        Makeshifts last the longest.

Re^2: variable set to 0 ? 0 : 1
by Aristotle (Chancellor) on Sep 08, 2002 at 04:35 UTC
    I usually use it to visually unclutter code that would otherwise be an if with tiny blocks. It can be used in void context for something like
    $i == 15 ? push @str, $_ : $j = $i += 1;
    or you can "chain" ternary operators by using another ternary as the false-branch of the previous:
    my $res = $cmd eq "add" ? $x + $y : $cmd eq "sub" ? $x - $y : $cmd eq "mul" ? $x * $y : $cmd eq "div" ? $x / $y : undef;
    which would otherwise have to be a hideously ugly and thrice as repetitive thing like this:
    my $res; if ($cmd eq "add") { $res = $x + $y } elsif($cmd eq "sub") { $res = $x - $y } elsif($cmd eq "mul") { $res = $x * $y } elsif($cmd eq "div") { $res = $x / $y } else { $res = undef }

    Makeshifts last the longest.

Re2: variable set to 0 ? 0 : 1
by blakem (Monsignor) on Sep 08, 2002 at 01:44 UTC
    Set $max to the maximum of $x vs $y:
    $max = $x > $y ? $x : $y;

    -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://195952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found