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


1 sort() is a builtin PERL function with three modes:
2 @result = sort @array; # equivalent to sort { $a cmp $b} @array;
3 which sorts the variables in @array using stringwise comparisons, returning them in @result
4 @result = sort BLOCK @array; # where statement BLOCK enclosed in {} curly brackets returns -1, 0, 1 given values of $a, $b
5 @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
6 @result = sort SUBNAME @array; # uses subroutine (which can be specified as value of scalar variable) to perform sorting
7 sub backsort { $b <=> $a; } # Reverse order for integers
8 @result = sort backsort @array; # sorts in numerically decreasing order

in Table To:


© 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