CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:



Jun 4, 2000, 1:16 PM

Post #1 of 6 (1189 views)
Sorting Can't Post

Can i alphabetically sort strings in perl?


Jun 4, 2000, 3:34 PM

Post #2 of 6 (1189 views)
Re: Sorting [In reply to] Can't Post

Sure. How are your strings stored?

Anyway, here are all sorts of ways to sort:

If you have an array of strings:
@colors = ("red", "blue", "white", "black", "green");
@sorted = sort @colors;
#Now, @sorted is a sorted versions of @colors

Note that capitals precede lowercase and numbers would not sort properly (e.g. 100 would come before 20).

However, sort supports the passing of a subroutine to customize how it sorts.

Here's a program that sorts numbers:

#!/usr/bin/perl -w
@prices = (99,10000,209,21,901);

sub numerically { $a <=> $b }

@sorted = sort numerically @prices;

# @sorted is now a 'numerically' sorted copy of @prices

Ok, there are a few things you need to know.

One, sort passes values to be tested to the routine as $a and $b (not as part of @_). Therefore, the routine needs to compare $a and $b and return a value based on their relative order.

If $a should appear before $b, the routine should return a negative value. If $b should be before $a, then it should return a positive value. If it doesn't matter, then it should return 0.

Ok, now what's with the $a <=> $b? Well, the <=> operator compares two numbers and returns either 1, 0 or -1 depending on if the first value is greater, equal or lesser.

It could just as well have been:

sub numerically { $a-$b }

You can use cmp instead of <=> for sorting text. For example:

sub alphabetically { $a cmp $b }

Or, if you want to ignore case....

sub alphabetically { lc($a) cmp lc($b) }

That's fine for arrays. However, if you want to sort a hash (well, not sort it, but access it as if it were sorted), you can use the following:

to access the keys in alphabetical order:
foreach $key (sort keys %hash) { print $key; }

to access the values in alphabetical order:
foreach $key (sort byvalue keys %hash) { print $key; }


Dr. Zed


Jun 4, 2000, 5:39 PM

Post #3 of 6 (1189 views)
Re: Sorting [In reply to] Can't Post

oh thanks but i just used something called gt and it worked


Jun 4, 2000, 5:56 PM

Post #4 of 6 (1189 views)
Re: Sorting [In reply to] Can't Post

gt (greater than) is to strings what > is to numbers.

For strings, use: .... For numbers, use:
gt (greater than)..... >
lt (less than)........ <
eq (equal to)......... ==
ne (not equal to)..... !=
ge (greater or equal). >=
le (lesser or equal).. <=

Dr. Zed


Jun 4, 2000, 6:08 PM

Post #5 of 6 (1189 views)
Re: Sorting [In reply to] Can't Post

cool thanks do you have icq? or aol? or aol instant messenger?


Jun 5, 2000, 1:31 PM

Post #6 of 6 (1189 views)
Re: Sorting [In reply to] Can't Post

ICQ# - 29498013

Dr. Zed


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives