Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

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

As promised, here's some skeleton code (and sample data) to get you started:

#!/usr/bin/perl use strict; use warnings; use feature qw/say/; my %data_hash = ( "key0.1" => [ { "key1.1" => 3.3, "key1.2" => 17.8, "key1.3" => -2.4, }, { "key1.4" => 5.1, "key1.5" => 13, }, { "key1.6" => -69, "key1.7" => 127, "key1.8" => 2.718, "key1.9" => 3.3, }, ], "key0.2" => [ { "key1.10" => 3.3, "key1.11" => 2.5, }, { "key1.12" => -33, }, ], ); my %flat_hash = (); foreach my $key0 (keys %data_hash) { foreach my $index (0 .. $#{$data_hash{$key0}}) { foreach my $key1 (keys %{$data_hash{$key0}->[$index]}) { my $new_key = "$key0-$index-$key1"; $flat_hash{$new_key} = { "value" => $data_hash{$key0}->[$index]->{$key1}, "key0" => $key0, "index" => $index, "key1" => $key1, }; } } } foreach (sort { $flat_hash{$a}->{"value"} <=> $flat_hash{$b}->{"value" +} } keys %flat_hash) { say $flat_hash{$_}->{"value"}, ": key0 = ", $flat_hash{$_}->{"key0"}, ", index = ", $flat_hash{$_}->{"index"}, ", key1 = ", $flat_hash{$_}->{"key1"}; }

I didn't integrate the %limit_hash part since it wasn't part of your question.

N.B. - you can probably do this more easily and naturally still using map, but I just came back from an Apple Family reunion and am not yet thinking in Perl again. ;)


In reply to Re^2: sorting hash of array of hashes by value by AppleFritter
in thread sorting hash of array of hashes by value by Special_K

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-10-22 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (85 votes). Check out past polls.

    Notices?