Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Hash value sorting by iHutch105

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (11)
    As of 2014-07-29 10:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (214 votes), past polls