perlquestion iHutch105 <p>Hey all. First time poster and new to Perl so go easy. ;-)</p> <p>I'm having trouble sorting a hash. Basically, I have a class, which contains a hash, as such:</p> <code> sub new { my $self = {}; my $class = shift; bless($self, $class); $self->{'_counts'} = {}; return $self; } </code> <p>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.</p> <p>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:</p> <code> sub SortValues { my $self = shift; my $key; foreach $key ( sort{ ${self->{'_counts'}}{$a}} <=> ${self->{'_counts'}}{$b}} } keys %{$self->{'_counts'}}) { # Print out stuff } } </code> <p>The code works but, as far as I can tell, it's (quite understandably) sorting the values numerically by their reference number.</p> <p>I've tried dereferencing the values $a and $b so that they look like this:</p> <code> #e.g for $a, $b would be the same ${${self->{'_counts'}}{$a}}} </code> <p>However, I get a "Can't use string ("nnnn") as a SCALAR ref while "strict refs" in use at etc, where "nnnn" is the value in the hash.</p> <p>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).</p> <p>Does anyone with a better knowledge of this (that's most of you) have any idea on how I can solve this?</p> <p>Many thanks in advance,</p> <p>iHutch</p>