http://qs321.pair.com?node_id=207282


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
    Can anybody ellaborate on this statement by Ovid. This small side note is interesting to me and i want to know more. I'm always interested in speeding up perl.

    "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"
  • Why is it not straight C code? What is it?
  • Does this apply to any subroutine, or is 'sort' a special case?
  • Also, does the same issue hold true under mod_perl?
  • This reminds me of the rumor i heard that case non-sensitive pattern matching is particularly slow (/foo/i). Any ideas on how i could find out more about perl syntax and speed?

      It's not straight C code because it's Perl. Perl's default sort algorithm(s) are implemented in C, not Perl. Anytime you reimplement a built-in function in Perl, it'll be slower than if you used the default. It doesn't matter if you use mod_perl, as the big gain there is avoiding startup time.

      Regexp optimization is another subject altogether!

Re: Re: Changing Perl's sort Default
by krusty (Hermit) on Oct 23, 2002 at 08:34 UTC
    # incidentally, why doesn't this trigger a bareword # warning under strict? @new = sort alphanumeric @old; sub alphanumeric { lc $a cmp lc $b }
    Because it's expecting either a named subroutine or a block of code. You haven't given it a bareword. You've given it what it expects to be a sub. If it can't find a subroutine named alphanumberic, it'll error out with... "Undefined subroutine in sort at blah.pl line blech"