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: Intermediate:
Sorting an hash array

 



jkepler
New User

Jul 25, 2014, 3:43 PM

Post #1 of 9 (1566 views)
Sorting an hash array Can't Post

I'm trying to sort an hash array alphabetically. Here's what I'm doing:


Code
my @keys = sort { $functions{$a} <=> $functions{$b} } keys(%functions); 
my @vals = @functions{@keys};


$info = "out.txt";
open(out,">$info");

print out "Key: $_ and Value: $functions{$_}\n" foreach (keys%functions);

close;


but I'm getting the wrong results...

Any idea why?

Kind regards,

Kepler


FishMonger
Veteran / Moderator

Jul 25, 2014, 5:33 PM

Post #2 of 9 (1561 views)
Re: [jkepler] Sorting an hash array [In reply to] Can't Post

Cross posted on perlmonks.

http://www.perlmonks.org/?node_id=1095109


Laurent_R
Veteran / Moderator

Jul 26, 2014, 4:35 AM

Post #3 of 9 (1558 views)
Re: [jkepler] Sorting an hash array [In reply to] Can't Post

Hi,

The single most important problem is that, in this line:


Code
print out "Key: $_ and Value: $functions{$_}\n" foreach (keys%functions);

you are not using the result of your sort, which is stored in the @keys array.

It should presumably be:

Code
print out "Key: $_ and Value: $functions{$_}\n" foreach @keys;


You did not say what the content of your %functions hash is, so using the <=> operator may or may not be right, depending on which type of sort you want (<=> is for sorting data numerically). If you really need alphatetical sort, then use the cmp function instead of <=>.

Your close statement is not doing anything useful since you don't pass it the filehandle. And BTW, you should rather use lexical filehandles and the 3-argument syntax for open, and also check the result of the open statement. Something like this:


Code
my $info = "out.txt";  
open my $out, ">", $info or die "Could not open $info $! ";
print $out "Key: $_ and Value: $functions{$_}\n" for @keys;
close $out;


As a final point, I would add that this line:

Code
my @vals = @functions{@keys};

isn't doing anything useful either, since you are never using @vals.

PS: Some of the errors above might have been detected by the compiler if you had used the following pragmas:

Code
use strict; 
use warnings;

which you should include in every single piece of code having more than 1 line that you write.


(This post was edited by Laurent_R on Jul 26, 2014, 4:42 AM)


kepler
Novice

Jul 26, 2014, 4:54 AM

Post #4 of 9 (1551 views)
Re: [Laurent_R] Sorting an hash array [In reply to] Can't Post

Hi,

Great code :)

But its sorting by the VALUES of the hash; I wanted by the KEYS.

Also, I've used the cmp operator.

Kind regards,

JKepler


FishMonger
Veteran / Moderator

Jul 26, 2014, 8:41 AM

Post #5 of 9 (1547 views)
Re: [kepler] Sorting an hash array [In reply to] Can't Post


Code
my %functions = ( banana => 'yellow', apple => 'red', grape => 'green'); 
my @keys = sort keys %functions;

print "Key: $_ and Value: $functions{$_}\n" foreach @keys;


output

Quote
Key: apple and Value: red
Key: banana and Value: yellow
Key: grape and Value: green


In the future, please put beginning level questions like this in the "Beginner" section.


(This post was edited by FishMonger on Jul 26, 2014, 8:46 AM)


kepler
Novice

Jul 26, 2014, 1:31 PM

Post #6 of 9 (1541 views)
Re: [FishMonger] Sorting an hash array [In reply to] Can't Post

Hi,

The code works fine, thanks.
As you can see in the internet - several posts from pro websites - most of the solutions dont work. I've tryed every single one of them. Your code does work perfectly...

In my modest opinion, this is not a begginer post.

But I'll follow by the rules of the forum - I'm no different from everyone else.

Thank you very much for the help and kindness.

Kind regards,

Kepler


FishMonger
Veteran / Moderator

Jul 26, 2014, 2:10 PM

Post #7 of 9 (1537 views)
Re: [kepler] Sorting an hash array [In reply to] Can't Post

The reason none of the other posted solutions on each of the sites worked was because the question/problem was not clearly stated. And, you never gave any info on how the solutions failed to meet your needs. Saying that you always got the "same disorded result" without explaining/showing what results you were getting and how that differed from what you wanted doesn't help us to understand what you needed.

Given the code and question you posted, we all thought you wanted to sort the values. It wasn't until you specifically stated that you wanted to sort the keys that we were able to provide you with a working solution.


(This post was edited by FishMonger on Jul 26, 2014, 2:11 PM)


kepler
Novice

Jul 26, 2014, 2:17 PM

Post #8 of 9 (1534 views)
Re: [FishMonger] Sorting an hash array [In reply to] Can't Post

Ok :) Thanks.

Kind regards,

Kepler


Laurent_R
Veteran / Moderator

Jul 27, 2014, 3:00 AM

Post #9 of 9 (1494 views)
Re: [kepler] Sorting an hash array [In reply to] Can't Post

You never stated that you wanted to sort by the keys and the code you've shown:

Code
my @keys = sort { $functions{$a} <=> $functions{$b} } keys(%functions);

is really aimed at sorting the hash by the values (and is doing it right). Compare it with the much simpler code to sort alphabetically by the keys:

Code
my @keys = sort keys %functions;

How could we guess that you gave yourself so much trouble of writing such a more complicated expression if you just wanted to sort the hash by the keys?

I should add that, aside from rewriting you sort command to obtain a goal that you did not state, most of the other comments I made on your code still apply completely.

 
 


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

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