I have the following dataset :
$name $id $value ##
A abc 2.1
A pqr 5.9
A xyz 5.6
B twg 2.5
B ysc 4.7
C faa 4.7
C bar 2.4
D foo 1.2
D kar 0.3
D tar 3.5
D zyy 0.1
For each $name, I need to extract $id with highest $value. I tried something like this:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my $infile;
my %multi_hash;
open ($infile, "test.txt") || die "can't open $infile\n";
while (my$line=<$infile>) {
my($name,$id,$val)= split(/\t/, $line);
$multi_hash{$name}{$id}=$val;
}
# print Dumper \%multi_hash;
foreach my $name_1(sort keys %multi_hash){
foreach my $id_1 (keys %{$multi_hash{$name_1}}) {
print "$name_1\t$id_1\t$multi_hash{$name_1}{$id_1}";
}
}
I want the output as :
A pqr 5.9
B ysc 4.7
C faa 4.7
D tar 3.5
What I am able to print is something which is already in the input file.
Could anyone help with improving my program?