Sort

Another commonly used array function in Perl is sort, that provides a way of ordering an array. By default the ordering algorithm is alphabetical ascending, but we can easily change it.

Say that we have a string array:
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
But we are not happy with its current element disposition. Sometimes we could just need the reverse function, that we can use in this way
for(reverse(@months)) { print "$_\n"; }
But normally we require a more complex rearrangement. If we want to have our months array starting alphabetically with Apr and ending at Sep, we can call sort in its standard way:

my @sorted = sort @months;
print "@sorted\n";

If we want to do something more sophisticated, we can specify an explicit sort routine:

my @revSort = sort { $b cmp $a } @months;
print "@revSort\n";

The sort routine in included in braces and its value is negative, zero or positive accordingly to the relative order of the two parameter, $a and $b, passed to it by sort. In this case we wanted to order the array using an alphabetically descendent order, so we simply reverted the normally used comparison, $a cmp %b.

If you wonder what cmp is, it is a function that behaves just like the old C strcmp() function. I hope you don't need more to be said on the matter.

If we apply the standard sort algorithm to an array of integer, we get the (maybe unexpected) result of ordering it in ascending alphabetical order:

my @numbers = (4, 65, 1, 23, 7);
my @alphaSort = sort @numbers;
print "@alphaSort\n";

Why this happens should be quite clear, when we think better to it. Perl converts automatically numbers to strings, if it finds them in a string context. And by default sort apply the cmp function to decide how to order the elements in the array.

To get the array ordered as we probabily expected it from the beginning, we use the perl operator <=> that behaves like cmp but expecting numbers in input:

my @numSort = sort { $a <=> $b } @numbers;
print "@numSort\n";

Chapter 3 of Beginning Perl by Simon Cozens is about arrays and associative arrays (hashes).

No comments:

Post a Comment