Hi all,
I am trying to make script to find the max value at the number that was decided by user.
The data has two colum [fw] and [rw] and I hope I can find the max values of each colum.
in put :
posi (not have in data)
Fw Rw
1 10 1
2 24 0
3 20 3
4 26 0
5 34 0
etc.......
I hope data : If I enter decided number at 10
num posi fw rw
1 5 34 0
2 4 26 0
3 2 24 3
4 3 20 0
5 1 10 1
I sued the script to sort data but I can not find the first value. I mean
5 1 10 0
#!/usr/bin/perl;
use strict;
use warnings;
use Data::Dumper;
my $n = 31;#Decided position
my $put = 'thu1.txt';
my $filename = 'hoi.txt';
open my $fh, '<', $filename or die "Failed to open $filename: $!";
open my $fh1, '>',$put or die "Failed to open $put: $!";
my $i =1;
while (<$fh>){
next if m/^#/;#Skip comment lines
my @array = ();
foreach (1 .. $n){
my $rec = <$fh>;
last unless defined $rec;
$rec =~ s/\s*$//;#Remove spaces or newline characters from end
push @array, $rec;
}
@array = sort {my ($sa, $sa1) = split /\s+/, $a;
my ($sb,$sb1) = split /\s+/, $b;
$sb <=> $sa;} @array;
foreach(@array){
print $fh1 "$i\t$_\n";
$i++;
}
}
and then I used small script to find the max value
#!/usr/bin/perl;
use strict;
use warnings;
my $put = 'thu1.txt';
open my $fh, '<', $put or die "Failed to open $put: $!";
print "nhap so vao ";
my $so =<STDIN>;
while (<$fh>){
chomp($fh);
my $rec = $fh;
my ($nu, $fw, $rw) =split (/\t/,$_);
chomp ();
#print "$po\n";
if ($nu <= $so) {print "$nu\t$fw\t$rw"}
}
Could you show me what wrong in that script and how to make it by one script? thank you very much.