Monks,

I have been trying to create a sorter for a semi-complex data structure. First I store the data structure using Storable, and then I retreive it. The data structure comes back as a hashref and I dereference it back to a hash. Basically the sorter does what its called. It either sorts by alpha of the keys (else), total (if) or value (elsif). The data structure itself looks as follows:

Data structure snippet:

$VAR3 = 'URIBL_OB_SURBL+MPART_ALT_DIFF_COUNT'; $VAR4 = { 'Total' => 10, 'Value' => '3.716' };
$VAR5 = 'BAYES_00+FORGED_RCVD_HELO'; $VAR6 = { 'Total' => 5, 'Value' => '-2.464' }; $VAR7 = 'BAYES_00+HTML_LINK_PUSH_HERE'; $VAR8 = { 'Total' => 1, 'Value' => '-2.202' }; $VAR9 = 'SPF_HELO_PASS'; $VAR10 = { 'Total' => 16, 'Value' => '-0.001' }; $VAR11 = 'BAYES_95+HTML_MIME_NO_HTML_TAG'; $VAR12 = { 'Total' => 1, 'Value' => '4.082' };

I included both methods that I attempted. I can't figure out how to get the sorter to sort (which is one of the primary problems). Sorter creation code:

if ($sort eq 't') { $sorter = sub { sort values %{ do { my %tosort; for my $test (keys %tests) { # Prints values correctly # Values go into hash correctly # print "Test: $test -- Total: ". $tests{$test}{"Total"} ."\n"; $tosort{$test} = $tests{$test}{"Total"}; } return \%tosort; }; } };
} elsif ($sort eq 'v') { $sorter = sub { sort values %{ sub { my %tosort; for my $test (keys %tests) { # Prints values correctly # Values go into hash correctly # print "Test: $test -- Total: ". $tests{$test}{"Value"} ."\n"; $tosort{$test} = $tests{$test}{"Value"}; } return \%tosort; }; } }; } else { $sorter = sub { sort keys %tests }; }

Regardless of the sorter being called, I want to be able to access the keys and the values so I can use them at a later moment.

while ( my ($test,$val) = each %{$sorter->()} ) { print "Test: ". $test Val: $val\n"; # Sorter value # Show me the assocaited %tests stuff print " Total: ". $tests{$test}{"Total"} ."\n"; print " Value: ". $tests{$test}{"Value"} ."\n\n"; }

Basically, the sorter doesn't sort the way I want to (unless its sorting by alpha key), and then assuming the sorter is sorted by "Total", the while loop doesn't give me the key/val of the information I need. And I believe the information I need is the key of the sorted value so I can use it to display the associated values in the %tests global hash.

I am sure that this is a little confusing and I tried my best to articulate the problem as best as possible. Thanks for the help.

Eric

Update: Thanks to everyone who aided me. I ended up using the following method:

sub Print_Tests($$) { my $sort = shift || "a"; my $order = shift || "d"; my (@ordered_keys); # Create the sorter if (($sort eq 't') && ($order eq "a")) { @ordered_keys = sort { $tests{$a}{"Total"} <=> $tests{$b}{"Total"} + } keys %tests; } elsif (($sort eq 't') && ($order eq "d")) { @ordered_keys = sort { $tests{$b}{"Total"} <=> $tests{$a}{"Total"} + } keys %tests; } elsif (($sort eq 'v') && ($order eq "d")) { @ordered_keys = sort { $tests{$b}{"Value"} <=> $tests{$a}{"Value"} + } keys %tests; } elsif (($sort eq 'v') && ($order eq "a")) { @ordered_keys = sort { $tests{$a}{"Value"} <=> $tests{$b}{"Value"} + } keys %tests; } elsif (($sort eq 'a') && ($order eq "a")) { @ordered_keys = sort keys %tests; } else { @ordered_keys = sort reverse keys %tests; } foreach my $key (@ordered_keys) { my $val = $tests{$key}; print "$key\n"; print " Total: $val->{Total}\n"; print " Value: $val->{Value}\n\n"; } }

In reply to Sorting a hash of a hash using anonymous subs by madbombX

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.