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

iHutch105 has asked for the wisdom of the Perl Monks concerning the following question:

Hey all. First time poster and new to Perl so go easy. ;-)

I'm having trouble sorting a hash. Basically, I have a class, which contains a hash, as such:

sub new { my $self = {}; my $class = shift; bless($self, $class); $self->{'_counts'} = {}; return $self; }

Basically, a few of these class may be created and each one will go through a bunch of files which contain dates and counts for those dates. Since multiple dates can span across the files, the counts for the dates as added to the hash entry for that date (where the date is the key and the count is the value). This is all working fine.

Now, I want to sort the hash by values. Undoubtedly there's a nicer way to do it than my code, but this is what I have at the moment:

sub SortValues { my $self = shift; my $key; foreach $key ( sort{ ${self->{'_counts'}}{$a}} <=> ${self->{'_count +s'}}{$b}} } keys %{$self->{'_counts'}}) { # Print out stuff } }

The code works but, as far as I can tell, it's (quite understandably) sorting the values numerically by their reference number.

I've tried dereferencing the values $a and $b so that they look like this:

#e.g for $a, $b would be the same ${${self->{'_counts'}}{$a}}}

However, I get a "Can't use string ("nnnn") as a SCALAR ref while "strict refs" in use at file.pm etc, where "nnnn" is the value in the hash.

I'm kind of going in circles a little trying to solve it now and I've had a search around to see if I can find a similar scenario, which I can't (or at least I don't have the knowledge to be able to apply anything I've found to my problem).

Does anyone with a better knowledge of this (that's most of you) have any idea on how I can solve this?

Many thanks in advance,

iHutch

Replies are listed 'Best First'.
Re: Hash value sorting
by daxim (Curate) on Aug 14, 2012 at 12:08 UTC

      Awesome, sorted with one minor adjustment to your code; I had to dereference the conditions of the sort because it was sorting by the scalar reference address. I changed it to:

      foreach my $key ( sort { ${$self->{_counts}{$a}} <=> ${$self->{_counts}{$b}} } keys %{ $self->{_counts} }

      Thanks so much for the help.

        Try this:

        foreach my $key ( sort { $self->{_counts}->{$a} <=> $self->{_counts}->{$b} } keys %{ $self->{_counts} }

        If you do $self->{_counts}{$b}, Perl thinks $self->{_counts} is a real Hash, not an HashRef. If you use the arrow to dereference, it is easier to read.

        Take my advice. I don't use it anyway.
        That code collides with your explanation of the data structure in 987336. Only one can be correct.

        In the constructor I see you initialise the field _counts with a hash reference. Where do you now get a scalar reference from?

        It would help if you dump $self before sorting, so we can see what the data structure really looks like.

        use Data::Dumper; print Dumper $self;

      Thanks! It looks kind similar to what I have. Maybe I'm screwing up somewhere with the dereferencing. I'll give that a shot.

Re: Hash value sorting
by moritz (Cardinal) on Aug 14, 2012 at 12:05 UTC

      Wow you guys are fast.

      The keys are dates, stored in hex form and the values are a count of how many times that date has appeared in a scan of a database.

      I'll see if I can cobble together a working example without reading from files.