Misplaced Pages

Spaceship operator: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 02:47, 19 July 2007 editMwtoews (talk | contribs)Extended confirmed users, Pending changes reviewers, Rollbackers27,455 edits update link← Previous edit Revision as of 21:59, 21 July 2007 edit undo69.86.190.131 (talk)No edit summaryNext edit →
Line 1: Line 1:
The '''spaceship operator''' is a binary ] which originated in the ] ]. ] also supports the spaceship operator. It is written <=> . Unlike traditional equality operators, which will return 1 or 0 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. The '''spaceship operator''' is a binary ] that originated in the ] ]. Other languages, such as ] and ] also support the spaceship operator. It is written <=> . Unlike traditional equality operators, which will return 1 or 0 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.


The spaceship operator is primarily used for comparisons in ]. It is usable for a ] as it will return 0 for equal arguments. The spaceship operator is primarily used for comparisons in ]. It is usable for a ] as it will return 0 for equal arguments.

Revision as of 21:59, 21 July 2007

The spaceship operator is a binary relational operator that originated in the Perl programming language. Other languages, such as Ruby_programming_language and Groovy_programming_language also support the spaceship operator. It is written <=> . Unlike traditional equality operators, which will return 1 or 0 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.

The spaceship operator is primarily used for comparisons in sorting. It is usable for a stable sort as it will return 0 for equal arguments.

The spaceship operator takes its name because it looks like a small flying saucer as ASCII art. The term is now commonly used and the operator is referred by the name within the actual perldocs.

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

External Links

Reference to the properties of the operator within perldoc

Reference to the actual term within perldoc

Category: