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