Basic HTML version of Foils prepared Sept 20 97

Foil 57 Sorting with Various Criteria

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


sort() is a builtin PERL function with three modes:
@result = sort @array; # equivalent to sort { $a cmp $b} @array;
which sorts the variables in @array using stringwise comparisons, returning them in @result
@result = sort BLOCK @array; # where statement BLOCK enclosed in {} curly brackets returns -1, 0, 1 given values of $a, $b
@result = sort { $age{$a} <=> $age{$b} } @array; # sorts by age if entries in @arrays are keys to hash %age, which holds numeric age for each key
@result = sort SUBNAME @array; # uses subroutine (which can be specified as value of scalar variable) to perform sorting
sub backsort { $b <=> $a; } # Reverse order for integers
@result = sort backsort @array; # sorts in numerically decreasing order



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Sep 21 1997