Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: Short form (ternary) if else

by Marshall (Canon)
on Feb 08, 2012 at 22:26 UTC ( [id://952607]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Short form (ternary) if else
in thread Short form (ternary) if else

This is the "spaceship" operator for comparing in a numeric sense:
#!/usr/bin/perl -w use strict; my $x = 2; my $y = 3; my $cmp_result = $x <=> $y; print $cmp_result # -1
Note: while naming variables the same as Perl key words is allowed (e.g. $cmp, (cmp is a key word), I personally try to avoid this).

for comparison in a string context, use cmp:

my $cmp_result = $stringX cmp $stringY;
Update: As to the history, I have no idea why <=> is called the "spaceship" operator, but that is the common usage.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 17:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found