Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Changing Perl's sort Default

by @rocks (Scribe)
on Oct 23, 2002 at 03:04 UTC ( [id://207271]=perlquestion: print w/replies, xml ) Need Help??

@rocks has asked for the wisdom of the Perl Monks concerning the following question:

Hey guy,

I know from reading Learning Perl 3rd Edition that changing Perl's default (ASCIIbetical) to your own custom method is possible. However, I don't know how to however because I promised myself that I would read the book in order no matter what and I would probably get confused if I skipped to chapter 15. Can anyone give me a clear and simple explanation of how to change the default? I really would like to know how to change it to Alphanumerical. ASCIIbetical is so odd.

-@rocks

Replies are listed 'Best First'.
Re: Changing Perl's sort Default
by Ovid (Cardinal) on Oct 23, 2002 at 03:32 UTC

    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.

      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!

      # 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"
Re: Changing Perl's sort Default
by graff (Chancellor) on Oct 23, 2002 at 03:27 UTC
    Probably the most concise, direct description of how to "customize" the behavior of sort can be found here:
    perldoc -f sort
    That will print out just the portion of the massive "perlfunc" man page that discusses the "sort" function.

    BTW, when you say "alphanumerical", do you really mean "case-folded"? The output of the above command contains the answer.

      Yes graff I did mean case folded. Thank you for the answeer and for your time.

      -@rocks

Re: Changing Perl's sort Default
by DigitalKitty (Parson) on Oct 23, 2002 at 03:30 UTC
    Hi @rocks,

    The sort function has two modes. One is numeric sort and the other is lexical sort. An example of each is below:

    #!/usr/bin/perl -w use strict; my @array = ( 2, 5, 17, 1, 28, 30, 100, -1 ); my @array2 = ( 'x', 'v', 'z', 'p', 'a', 'b' ); my @array_sorted = (); my @array2_sorted = (); @array1_sorted = sort { $a <=> $b } @array; @array2_sorted = sort { $a cmp $b } @array2; print "@array1_sorted\n"; print "@array2_sorted\n"; __OUTPUT__ C:\perl>perl p5.pl -1 1 2 5 17 28 30 100 a b p v x z C:\perl>

    Hope this helps,
    -Katie.
      Katie, I think you misunderstood my question. I wanted to know if there was a to change the default of perl's sort command structure to AlphaNumerical or any other custom setting.

      -@rocks

Re: Changing Perl's sort Default
by fglock (Vicar) on Oct 23, 2002 at 20:00 UTC

    This is one way to "change the default", but i think is a bit odd:

    sub sort { sort { $a <=> $b } @_ } @a=qw(1 12 11 102); @a = &sort (@a); print "@a";' Output: 1 11 12 102

    It works because &sort calls main::sort, instead of CORE::sort

      This is the "numerical + alphabetical" version:

      sub sort { sort { ($a <=> $b) or ($a cmp $b) } @_ } @a=qw( 1 12 11 102 ddd abc cba 1c 1a ); @a = &sort (@a); print "@a\n"; Output: abc cba ddd 1 1a 1c 11 12 102

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://207271]
Approved by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-18 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found