< Perl Programming < Keywords

The code
The cmp keyword
cmp is a binary function that returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument. In other words, cmp does the same textually what the binary function <=> does.
Syntax
EXPRESSION cmp EXPRESSION
Examples

use strict;
use warnings;
my @array = ("Hotel", "Alpha", "Foxtrott", "Bravo", "India", "Charlie", "10", "-10", "9", "-9", "Echo", "Delta");
my @sorted_array = (sort { $a cmp $b } @array);
print join(",", @sorted_array), "\n";
returns array contents sorted alphabetically:
-10, 10, Alpha, Bravo, Charlie, Delta, Echo, Foxtrott, Hotel, India
See also
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.