in reply to Changing Perl's sort Default
If your read perldoc -f sort, you should learn all that you need to know. If you have 5.8 (?) or later, you can also read about the sort pragma with perldoc sort.
I'm not sure how you are defining an alphanumeric sort (do you want digits before or after alphas?), so for the purpose of simplicity, I'll assume that you want a case-insensitive sort so that you don't have to worry worry about 'a' coming after 'Z'. To do this, you provide a sort subroutine. There are various ways to do this.
@new = sort { lc $a cmp lc $b } @old;
Some people prefer not to use an anonymous subroutine, so you have the option of naming it.
# incidentally, why doesn't this trigger a bareword # warning under strict? @new = sort alphanumeric @old; sub alphanumeric { lc $a cmp lc $b }
Again, read through the docs for more information on this.
As a small side note, be aware that providing a block or subroutine to the sort function will automatically slow down its performance as is will no longer be relying on straight C code to perform the sort. Naturally, I rarely feel it's necessary to consider this issue, but it's nice to know.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Changing Perl's sort Default
by true (Pilgrim) on Oct 23, 2002 at 04:27 UTC | |
by chromatic (Archbishop) on Oct 23, 2002 at 05:01 UTC | |
Re: Re: Changing Perl's sort Default
by krusty (Hermit) on Oct 23, 2002 at 08:34 UTC |