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. ;)
-
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 How to display code and escape characters
are good places to start.