Revision as of 16:58, 26 August 2013 editRory O'Kane (talk | contribs)495 edits fix grammar, reorder paragraphs← Previous edit | Revision as of 02:13, 11 September 2013 edit undoYobot (talk | contribs)Bots4,733,870 editsm WP:CHECKWIKI error fixes using AWB (9475)Next edit → | ||
Line 1: | Line 1: | ||
The '''spaceship operator''', written <code><=></code>, is a binary ] that originated in the ] ]. Other languages, such as ] and ], also support the spaceship operator. Unlike traditional equality operators, which will return 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or −1 depending on the value of the left argument relative to the right argument. If the left argument is greater than the right argument, the operator returns 1. If the left argument is less than the right argument, the operator returns −1. If the two arguments are equal, the operator returns 0. If the two arguments cannot be compared (e.g. one of them is ]), the operator returns <code>undef</code>. |
The '''spaceship operator''', written <code><=></code>, is a binary ] that originated in the ] ]. Other languages, such as ] and ], also support the spaceship operator. Unlike traditional equality operators, which will return 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or −1 depending on the value of the left argument relative to the right argument. If the left argument is greater than the right argument, the operator returns 1. If the left argument is less than the right argument, the operator returns −1. If the two arguments are equal, the operator returns 0. If the two arguments cannot be compared (e.g. one of them is ]), the operator returns <code>undef</code>. | ||
As a formula: | As a formula: | ||
Line 34: | Line 34: | ||
==External links== | ==External links== | ||
{{wikibooks|Perl Programming}} | {{wikibooks|Perl Programming}} | ||
* | * | ||
* | * |
Revision as of 02:13, 11 September 2013
The spaceship operator, written <=>
, is a binary relational operator that originated in the Perl programming language. Other languages, such as Ruby and Groovy, also support the spaceship operator. Unlike traditional equality operators, which will return 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or −1 depending on the value of the left argument relative to the right argument. If the left argument is greater than the right argument, the operator returns 1. If the left argument is less than the right argument, the operator returns −1. If the two arguments are equal, the operator returns 0. If the two arguments cannot be compared (e.g. one of them is NaN), the operator returns undef
.
As a formula:
In Perl, the <=>
operator only performs numeric comparisons. For string-based comparison, the analogous cmp
operator is used instead.
The spaceship operator is primarily used for comparisons in sorting.
The spaceship operator takes its name because it resembles Darth Vader's fighter from Star Wars Episode IV: A New Hope . The term is now commonly used and the operator is referred by the name within the Perl documentation.
This operator is also used in ASCII-based mathematical notation to represent "less than, equal to or greater than", and is synonymous with the symbols ⋛ and ⋚. It can be used to test if the result of a calculation is actually a number.
Example
$a = 5 <=> 7; # $a is set to -1 $a = 7 <=> 5; # $a is set to 1 $a = 6 <=> 6; # $a is set to 0
See also
External links
- Reference to the properties of the operator within perldoc
- Reference to the actual term within perldoc