in reply to help in sort
I wonder if you'd care to suggest how we could improve the documentation for sort to make it more obvious how to solve your problem. Currently the docs say this:
... If the subroutine's prototype is "($$)", the elements to be compared are passed by reference in @_, as for a normal subroutine. This is slower than unprototyped subroutines, where the elements to be compared are passed into the subroutine as the package global variables $a and $b (see example below). Note that in the latter case, it is usually counter-productive to declare $a and $b as lexicals. ...Examples: # sort lexically @articles = sort @files; # same thing, but with explicit sort routine @articles = sort {$a cmp $b} @files; # now case-insensitively @articles = sort {uc($a) cmp uc($b)} @files; # same thing in reversed order @articles = sort {$b cmp $a} @files; # sort numerically ascending @articles = sort {$a <=> $b} @files; # sort numerically descending @articles = sort {$b <=> $a} @files;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: help in sort
by uva (Sexton) on Feb 08, 2006 at 13:53 UTC | |
by jonadab (Parson) on Feb 08, 2006 at 14:02 UTC | |
by uva (Sexton) on Feb 08, 2006 at 14:08 UTC |