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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Sort Question

 



fox
Novice

Jun 26, 2003, 9:31 PM

Post #1 of 4 (1456 views)
Sort Question Can't Post

Hi,

How can I sort a hash that has keys like this

$res{"1 "."123456789"." 1"}=$tmp;
$res{"1 "."124456789"." 2"}=$tmp;
$res{"2 "."125456789"." 3"}=$tmp;
$res{"2 "."126456789"." 4"}=$tmp;
$res{"3 "."127456789"." 5"}=$tmp;
$res{"3 "."128456789"." 6"}=$tmp;

I need to sort %res by the middle section there.

I'm using the simple
foreach $key (sort keys %res){

}

but it cant sort by the middle section properly because of the beginning and trailing difference.

I was hoping I could learn some advanced sorting and maybe map would come into use because i never learned to use the map function. Still today after 4 years It's not clear to me,

Oh, and the first part of the hash is for an order, so it has to keep it in mind in the sorting process. The keys that begin with 1 are to be displayed at the top of the results, and then the results marked 2nd and 3rd. It works great for everything in my search engine, except it can handle sorting by the timestamp. And the trailing number is just something different to keep the key unique.

Does this make sense?

Thanks,
Tony


davorg
Thaumaturge / Moderator

Jun 27, 2003, 1:04 AM

Post #2 of 4 (1454 views)
Re: [fox] Sort Question [In reply to] Can't Post

It's not really clear which parts of your key you want to sort by. you start by saying "I need to sort %res by the middle section there" but later you say "the first part of the hash is for an order, so it has to keep it in mind in the sorting process. The keys that begin with 1 are to be displayed at the top of the results" so I'm a little confused.

Whatever you need to do, the "sort" function can do it. You just need to write your own custom sorting routine.

Code
foreach my $key (sort my_sort keys %res) { 
# do stuff
}

sub my_sort {
# split $a and $b into the separate sections
my @a = $a =~ /(\d)(\d+)(\d)/;
my @b = $b =~ /(\d)(\d+)(\d)/;

# Sort by whichever bits you want.
# In this example, I've sorted by the
# middle section, followed by the first digit.
return $a[1] <=> $b[1] || $a[0] <=> $b[0];
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


fox
Novice

Jun 27, 2003, 3:20 AM

Post #3 of 4 (1451 views)
Re: [davorg] Sort Question [In reply to] Can't Post

 
Hi Dave

Thanks a lot for the tip, I'm working with it right now,

I didn't know that I could use a sub in a sort function, how cool!

I loved the Greek History article on your site, haha. Classic!

I didn't know about that intel attorny, what a dolt.

Tony


davorg
Thaumaturge / Moderator

Jun 27, 2003, 3:36 AM

Post #4 of 4 (1449 views)
Re: [fox] Sort Question [In reply to] Can't Post


In Reply To
I didn't know that I could use a sub in a sort function, how cool!


Maybe you should read the docs on sort then :)

Or there's a good section in Data Munging with Perl.


In Reply To
I loved the Greek History article on your site, haha. Classic!


Er... that's geek history. The difference is important.


In Reply To
I didn't know about that intel attorny, what a dolt.


See the Friends of Randal Schwartz page for more details.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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