PDA

View Full Version : getting minimum values


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

mlseim
03-15-2006, 12:11 AM
This is not a direct answer to your question, but you
might find a use for this in the future (or anyone else
may find this useful):

http://www.scatterchart.com/calculator/statistical_calculator.html

This is a method for doing some calculations without using a module.

FishMonger
03-15-2006, 04:54 PM
#!/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];
$best{$n} = $_ if (!exists($best{$n}) || ($s < $best{$n}));
}
close (IF1);

foreach $key (sort keys %best) {
print $best{$key};
}