RE: Spaceship Operator - I canna git it t' worrrk, cap'n!

by "Jason Brett" <jbbrett(at)zigzagnet.com>

 Date:  Thu, 6 Apr 2000 19:54:53 -0400
 To:  "Quackamoe T-Bird-S" <quackamoe(at)yahoo.com>,
<hwg-languages(at)hwg.org>
 In-Reply-To:  yahoo
  todo: View Thread, Original
The code you are using is being thrown off by the revision letters! You are
not passing the sort function a list of numbers but a list of strings (once
the revision letters are added, it is no longer a number.) There are a
couple of ways you can handle this, and this may not be the easiest, but its
what comes to mind without looking anything up, and be forewarned, I am
intermediate skill level, and I'm doing it without testing.

# first, for what we are doing, its best to slurp all the data into an array
like this:

@wordlist = <FILEHANDLE>;

# the list you need to sort is going to be first sorted numerically, and if
it can't be sorted
# numerically, then we'll sort it by ASCII value. Put it in a subroutine for
the sort function

# of course this subroutine can go anywhere in your code, I just put it here
for readability.

sub with_revision {

($a <=> $b) || ($a cmp $b); # sort by number if you can, or else compare
ascii value

# now for the magic. . .

@sortedlist = sort with_revision @wordlist; #sort can take a subroutine as
an argument
# but don't use parentheses

# now print the list to see if we did it right!

print "@sortedlist";

Hope this helps!

Regards,

Jason Brett
ZigZag Internet Solutions
Powerful Solutions. Explosive Results.


> -----Original Message-----
> From: owner-hwg-languages(at)hwg.org [mailto:owner-hwg-languages(at)hwg.org]On
> Behalf Of Quackamoe T-Bird-S
> Sent: Thursday, April 06, 2000 4:40 PM
> To: hwg-languages(at)hwg.org
> Subject: Spaceship Operator - I canna git it t' worrrk, cap'n!
>
>
> Right here in front of me is _Learning Perl_. On
> p. 170 is the following code to get an array of
> numbers sorted numerically (1,2,3,11,111,21,30, etc
> instead of 1,11,111,2,21,3,30, etc):
>
>   @rightlist = sort { $a <=> $b } @wronglist;
>
> And right here on my Ultra monitor screen I have
> the following code to do the same thing:
>
> #!/apps/LWperl/bin/perl -w[1]
> while (<>){     # reads the file of numbers
> @wordlist = $_;
> @sortwordlist = sort { $a <=> $b } @wordlist;
> print @sortwordlist;
> }
>
> The file being read in is a list of numbers with
> revision letters or a dash like so:
>
>   5335 -
>   5336 A
>   5337 B
>   5338 -
>
> There's 2354 numbers. I just want them sorted in
> numeric order. What am I doing wrong? Is it the
> revision letters? I tried
>
>   @sortwordlist = sort { $a cmp $b } @wordlist;
>
> Also no luck. Suggestions?
>
> Terry Fowler
> Boeing
> [1] No matter how I try I can't get the SysAdmin to
>     install perl in /usr/bin/perl. Go figure.
>
> __________________________________________________
> Do You Yahoo!?
> Talk to your friends online with Yahoo! Messenger.
> http://im.yahoo.com
>

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA