Solved

Puzzle in sort

Posted on 2000-04-29
3
192 Views
Last Modified: 2010-03-05
Hi,

I am not familiar with perl, and has came across this line for a sort

foreach $key (sort {$info{$b} <=> $info{$a}}keys(%info))
{

I was told that this is used to sort a hash list by using other fields that the key, how is it so?

Thanks in Advance.
0
Comment
Question by:sata
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2762184
perldoc -f sort
perldoc -q sort
0
 
LVL 3

Accepted Solution

by:
guadalupe earned 80 total points
ID: 2762350
OK the deal is this ... first $a and $b are identifiers to the keys supplied by the keys fuction applied to the hash %info at the right of the statement.  The <=> is a numerical comparative operator... that means that we understand the values of the hash to be numeric.  If we wanted to do alphabetic comparisons we would change the <=> for cmp    Then  we yor comment about  
"hash list by using other fields that the key"

is so because you will notice that the values to the left and right of the comparison operator are indeed not the keys but the values...  as mentioned above $a and $b identify the keys and as in any hash operation $hash{key} will be the value, so
$info{$b} and $info{$a} are indeed the values of the hash corresponding to the keys   $a   and $b ...

the real magic is in the $a, $b and sort these mantain two distinct keys and keep sorting untill the whole thing is sorted and there is just perl magic which I don't pretend to understand the internal workings of but hey thats what perl is for...if you wanted it another way you would be using C!  The final thing:  foreach $key....
All the rest before returns a sorted list of keys so you just take them one at a time and go!!!  The only other thing is that if I remember correctly
$info{$b} <=> $info{$a} will give lesser to greater  either way if it orders the wrong way from wat you wnt you can always chnge it to:

$info{$a} <=> $info{$b}  and the same if you were using cmp:

$info{$a} cmp $info{$b}...

Hope this has made things a bit clearer!
0
 
LVL 1

Author Comment

by:sata
ID: 2765368
Thanks for the answer!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now