cocoacat
03-14-2006, 11:36 AM
I wrote the code for sorting minimum value of each data. Source code is showed in below.
#!/usr/local/bin/perl
open(IF1, "test.txt") or die "Error opening data file: $!\n";
my $name_col =1;
my $score_col=3;
while (<IF1>)
{
my @col = split;
($n,$s) = @col[$name_col, $score_col];
if (!exists($max{$n}))
{
push (@names, $n);
}
if (!exists($max{$n}) || ($s < $max{$n}))
{
$max{$n} = $s;
$best{$n} = ();
}
if ($s == $max{$n})
{
$best{$n} .= "$_";
}
}
close (IF1);
foreach $n(@names)
{
print $best{$n};
}
===== test.txt =====
A CBP2 T01 e-155
A CBP2 T02 e-114
A CBP2 T03 e-115
A CBP3 T04 2e-155
A CBP3 T05 3e-155
***** Output *****
A CBP2 T01 e-155
A CBP2 T02 e-114
A CBP2 T03 e-115
A CBP3 T04 2e-155
--------------------------------
I have some problem about number like e-155, e-114, e-115. I would like to get only minimum value of each data (column2) but it is extracted all of them. Please help me for improving my code. Thank you very much.
\\\\\expected output\\\\\
A CBP2 T01 e-155
A CBP3 T04 2e-155
#!/usr/local/bin/perl
open(IF1, "test.txt") or die "Error opening data file: $!\n";
my $name_col =1;
my $score_col=3;
while (<IF1>)
{
my @col = split;
($n,$s) = @col[$name_col, $score_col];
if (!exists($max{$n}))
{
push (@names, $n);
}
if (!exists($max{$n}) || ($s < $max{$n}))
{
$max{$n} = $s;
$best{$n} = ();
}
if ($s == $max{$n})
{
$best{$n} .= "$_";
}
}
close (IF1);
foreach $n(@names)
{
print $best{$n};
}
===== test.txt =====
A CBP2 T01 e-155
A CBP2 T02 e-114
A CBP2 T03 e-115
A CBP3 T04 2e-155
A CBP3 T05 3e-155
***** Output *****
A CBP2 T01 e-155
A CBP2 T02 e-114
A CBP2 T03 e-115
A CBP3 T04 2e-155
--------------------------------
I have some problem about number like e-155, e-114, e-115. I would like to get only minimum value of each data (column2) but it is extracted all of them. Please help me for improving my code. Thank you very much.
\\\\\expected output\\\\\
A CBP2 T01 e-155
A CBP3 T04 2e-155