home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5321 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  2.2 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!network.ucsd.edu!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: perl sort bug on Irix?
  5. Message-ID: <1992Aug14.163959.17905@netlabs.com>
  6. Date: 14 Aug 92 16:39:59 GMT
  7. References: <93641@bu.edu>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 37
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <93641@bu.edu> jdh@bu-pub.bu.edu (Jason Heirtzler) writes:
  14. : Perl's sort seems to behave oddly on Irix.  What I would like
  15. : to do is an inverse sort based on the length.  Is this program
  16. : correct perl (ie. not some implementation dependant thing?)
  17. :   #!/usr/local/bin/perl --    # -*-Perl-*-
  18. :   push( @dirs, "usr" );
  19. :   push( @dirs, "usr/bin" );
  20. :   push( @dirs, "usr/bin/X11" );
  21. :   push( @dirs, "usr/lib" );
  22. :   @crud =  sort {length($a) < length($b);} @dirs;
  23.  
  24. Using < only gives a two state answer (0 and 1).  Sort subroutines need
  25. to supply a three state answer (<0, 0, >0).  It just so happens :-)
  26. that there's now a <=> operator (called the spaceship operator for
  27. reasons that will be obvious to anyone who's seen Star Wars) that
  28. returns (-1,0,1).  So the preferred solution in recent Perls would be
  29.  
  30.     @crud = sort { length($b) <=> length($a) } @dirs
  31.  
  32. (Note that we reverse $a and $b to reverse the ordering, since the spaceship
  33. operator only orders in the ascending direction.  (Likewise for "cmp", which
  34. orders strings rather than numbers.))
  35.  
  36. : What is the appropriate way to do the same sort on previous
  37. : releases?  At patch level 10 perl bawks at the sort syntax
  38. : (guess it's new syntax.)
  39.  
  40. Well, hey, if you can invent a nifty new work like "bawk"...  :-)
  41.  
  42. As you switch to progressively older versions, you'd have to supply a
  43. semicolon at the end of the block, remove the block into a separate
  44. subroutine definition, and change the spaceship operator to a minus.
  45.  
  46. : "Congratulations.  You aren't running Eunice."
  47.  
  48. In actual fact, I have nothing much against Eunice.  I think the WG did
  49. remarkably well considering the host environment.
  50.  
  51. You can verify this by looking at the other leg of the conditional in
  52. Configure.  If you ARE running Eunice, it's not Eunice that Configure
  53. complains about.
  54.  
  55. Larry
  56.