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