Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] Code Snippet Question

Expand Messages
  • Adrian Stovall
    the default comparison for sort is {$a cmp $b} which sorts based on the ascii values of the characters in a string, so a list like: 1 2 3 4 5 6 7 8 9 10 gets
    Message 1 of 7 , Sep 25, 2003
    • 0 Attachment
      the default comparison for sort is {$a cmp $b} which sorts based on the
      ascii values of the characters in a string, so a list like:

      1 2 3 4 5 6 7 8 9 10

      gets sorted as:

      1 10 2 3 4 5 6 7 8 9

      unless you specify a sort based on a numeric comparison like:

      {$a <=> $b}

      type "perldoc -f sort" for an introduction to the grisly details.

      > -----Original Message-----
      > From: Jeff 'japhy' Pinyan [mailto:japhy@...]
      > Sent: Thursday, September 25, 2003 1:27 PM
      > To: 'perl-beginner@yahoogroups.com'
      > Subject: RE: [PBML] Code Snippet Question
      >
      >
      > On Sep 25, Van Andel, Robbert said:
      >
      > >Thanks. I guess I'm having trouble understanding the
      > necessity of the
      > >{$a <=> $b} portion. What is the point of comparing two values?
      > >Obviously sort will sort the array so why the rest?
      >
      > You misunderstand. The code block { $a <=> $b } defines HOW
      > sort() will sort the array. sort() compares two elements at
      > a time, and the sort block tells it HOW to do that. If you
      > wanted to sort the elements of an array by their length, you might do:
      >
      > @sorted = sort { length($a) <=> length($b) } @data;
      >
      > --
      > Jeff "japhy" Pinyan japhy@...
      > http://www.pobox.com/~japhy/
      > RPI Acacia brother #734
      > http://www.perlmonks.org/ http://www.cpan.org/
      > <stu> what does y/// stand for? <tenderpuss> why,
      > yansliterate of course. [ I'm looking for programming work.
      > If you like my work, let me know. ]
      >
      >
      > ------------------------ Yahoo! Groups Sponsor
      > ---------------------~--> Upgrade to 128-Bit SSL Security!
      > http://us.click.yahoo.com/p7cEmB/s7qGAA/yigFAA> /ndFolB/TM
      >
      >
      > --------------------------------------------------------------
      > -------~->
      >
      > Unsubscribing info is here:
      > http://help.yahoo.com/help/us/groups/groups-> 32.html
      >
      > Your
      > use of Yahoo! Groups is subject to
      > http://docs.yahoo.com/info/terms/
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.