I'm trying to sort values stored in a hash from a large dataset similar to the following:
Id, Loc, Value, Etime
N1:1, S5H1, 2.0, 1112146781
N1:1, S23H1, 1.2, 1112198223
N1:1, S100H1, 1.1, 1112149258
N1:1, S19H2, 0.9, 1112176607
Sorted by Loc, all H1 come before H2 ordered by the middle term with respect to Id.
I'm not too familiar with map, but I believe it's what is needed. I just haven't
figured out how to make it do what I need it to and would like some help.
Here's is my test code snippet and a small dataset:
#!/usr/bin/perl -w
use strict;
my %dataset;
while (<>) {
# $_ => Id, Loc, Value, Etime
my ($id, $loc, $value, $etime) = split(/,\s+/, $_);
$dataset{$id}{$etime} = { "Value" => $value,
"Loc" => $loc,
};
}
foreach my $id (sort keys %dataset) {
foreach my $etime ( sort { $dataset{$id}{$a}{Loc} cmp $dataset{$id}
+{$b}{Loc} } keys %{$dataset{$id}}) {
my $loc = $dataset{$id}{$etime}{Loc};
my $value = $dataset{$id}{$etime}{Value};
printf(STDOUT "%2s, %6s, %.1f, %i\n", $id, $loc, $value, $etime)
+;
}
}
Input dataset:
N1:1, S100H1, 1.1, 1112149258
P1:1, S10H1, 1.1, 1112149258
N3:2, S102H1, 1.1, 1112149258
P4:2, S10H1, 1.1, 1112149258
N1:1, S19H2, 0.9, 1112176607
P1:1, S9H2, 0.9, 1112176607
N3:2, S29H2, 0.9, 1112176607
P4:2, S10H2, 0.9, 1112176607
N1:1, S23H1, 1.2, 1112198223
P1:1, S2H1, 1.2, 1112198223
N3:2, S33H1, 1.2, 1112198223
P4:2, S21H1, 1.2, 1112198223
N1:1, S5H1, 2.0, 1112146781
P1:1, S15H1, 2.0, 1112146781
N3:2, S5H1, 2.0, 1112146781
P4:2, S1H1, 2.0, 1112146781
Desired output:
N1:1, S5H1, 2.0, 1112146781
N1:1, S23H1, 1.2, 1112198223
N1:1, S100H1, 1.1, 1112149258
N1:1, S19H2, 0.9, 1112176607
N3:2, S5H1, 2.0, 1112146781
N3:2, S33H1, 1.2, 1112198223
N3:2, S102H1, 1.1, 1112149258
N3:2, S29H2, 0.9, 1112176607
P1:1, S2H1, 1.2, 1112198223
P1:1, S10H1, 1.1, 1112149258
P1:1, S15H1, 2.0, 1112146781
P1:1, S9H2, 0.9, 1112176607
P4:2, S1H1, 2.0, 1112146781
P4:2, S10H1, 1.1, 1112149258
P4:2, S21H1, 1.2, 1112198223
P4:2, S10H2, 0.9, 1112176607
Thanks,
Ryan
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.