You really need to try to make your question as clear as possible - I don't really know what you're asking, but here's _my_ guess. You want to know how to sort the %games hash by the "count" part of the value. The only way I know of to sort by part of the value is to get the part(s) of the value you want to sort by, and create a new hash where the "sort" part of the value is the key, and the value is the whole value - something like this:
#!C:\Perl\bin use strict; my %games = ( 'john' => '11::Boston vs. Orlando::2004-01-15', 'beth' => '20::Minesota vs. New York::2004-01-15', 'bob' => '5::San Antonio vs. Pittsburgh::2004-01-13' ); my %sorted_on_count_hash = (); while (my($person,$value) = each %games) { my ($count,$game,$date) = split /::/, $value; my $sorted_key = sprintf("%04d", $count); # Zero fill count # so that numeric # part of key is same # length, so that sort # is correct based on # count. $sorted_key .= $person; # add person to key to make key unique # for that person $sorted_on_count_hash{$sorted_key} = { "count" => $count, "game" => $game, "date" => $date }; } my %sorted_on_count_hash = (); foreach my $key (sort keys %sorted_on_count_hash) { print "count=[$sorted_on_count_hash{$key}{'count'}], " . "game=[$sorted_on_count_hash{$key}{'game'}], " . "date=[$sorted_on_count_hash{$key}{'date'}]\n"; }
and here is the output:
------------------------------------------------------ count=[5], game=[San Antonio vs. Pittsburgh], date=[2004-01-13] count=[11], game=[Boston vs. Orlando], date=[2004-01-15] count=[20], game=[Minesota vs. New York], date=[2004-01-15] ------------------------------------------------------
HTH.

In reply to Re: Sorting on a split hash value by hmerrill
in thread Sorting on a split hash value by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.