in reply to
Help needed sorting data with multiple associated hashes
One way to do it is to create another hash to map the parts to the key:
my %table;
my %map;
foreach my $key (sort keys %Data)
{
my $lValue = $Data{$key};
my @array = split ' ', $lValue;
my $lPartStart = $array[2];
my $lPartEnd = $array[4];
my $lReference = $array[8];
chop($lReference);
$table{$lReference} = [] unless exists $table{$lReference};
push @{$table{$lReference}}, $lPartStart;
$map{$lReference}{$lPartStart} = $key; # THIS LINE ADDED
}
foreach my $lReference (sort keys %table)
{
print "Ref: $lReference, parts: ";
my @parts = @{$table{$lReference}};
print join ', ', sort @parts;
print "\n";
}
foreach my $lReference (sort keys %table)
{
print "Ref: $lReference, lines: ";
my @parts = @{$table{$lReference}};
my @lines = map { $map{$lReference}{$_} } sort @parts;
print join ', ', @lines;
print "\n";
}
Output:
Ref: 1, parts: 1, 2, 3
Ref: 2, parts: 2, 3, 4
Ref: 1, lines: 5, 4, 6
Ref: 2, lines: 17, 18, 16