So, why not prepend the key to the list in the first loop instead:
#!/usr/bin/env perl
use strict;
use warnings;
my %seen;
$/ = "";
while (<>) {
chomp;
my ($key, $value) = split ('\t', $_);
my @lines = split /\n/, $key;
my $key1 = $lines[1];
$seen{$key1} //= [ $key ];
push (@{$seen{$key1}}, $value);
}
foreach my $key1 (sort keys %seen) {
print join ("\t", @{$seen{$key1}});
}
If this doesn't do it for you then perhaps you might state what grander problem it is that you are trying to solve and a better overall approach might be in order. |