Basic HTML version of Foils prepared March 30 97

Foil 54 Sorting with various criteria (Chapter 15 of Llama Book)

From PERL4 Tutorial for CPS616 Computational Science for Information Age Course CPS616 -- February 1995. by Geoffrey C. Fox


1 sort() is a builtin PERL function with three modes:
2 @result = sort @array; # equivalent to sort { $a cmp $b} @array;
3 and sorts using stringwise comparisons, the variables in @array 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 associative array %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 Sat Sep 6 1997