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.

Recommended Answers

All 4 Replies

Not really sure what is going now, but I will take a guess. I think this does what you want. The first value was lost in the read at the top of the while loop. That is why I changed the order where $rec is read.
Hope this helps.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Not sure what this is really used for my $n = 31; #Decided position my $filename = 'hoi.txt'; open my $fh, '<', $filename or die "Failed to open $filename: $!"; my $i =1; # Why this loop and the other? while (my $rec = <$fh>){ next if $rec =~ m/^#/; #Skip comment lines my @array = (); # Again, not sure why the loop in here # Why one $n times? foreach (1 .. $n){
        last unless defined $rec; 
        $rec =~ s/\s*$//; #Remove spaces or newline characters from end
        push @array, $rec; 
        $rec = <$fh>; } @array = sort { my ($sa, $sa1) = split /\s+/, $a;
        my ($sb,$sb1) = split /\s+/, $b;
        $sb <=> $sa;
        } @array; print "nhap so vao "; my $so =<STDIN>; foreach (@array){
       print "$i\t$_\n";
       last if ($i++ >= $so); } } close($fh);

Not really sure what is going now, but I will take a guess. I think this does what you want. The first value was lost in the read at the top of the while loop. That is why I changed the order where $rec is read.
Hope this helps.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Not sure what this is really used for my $n = 31; #Decided position my $filename = 'hoi.txt'; open my $fh, '<', $filename or die "Failed to open $filename: $!"; my $i =1; # Why this loop and the other? while (my $rec = <$fh>){ next if $rec =~ m/^#/; #Skip comment lines my @array = (); # Again, not sure why the loop in here # Why one $n times? foreach (1 .. $n){
        last unless defined $rec; 
        $rec =~ s/\s*$//; #Remove spaces or newline characters from end
        push @array, $rec; 
        $rec = <$fh>; } @array = sort { my ($sa, $sa1) = split /\s+/, $a;
        my ($sb,$sb1) = split /\s+/, $b;
        $sb <=> $sa;
        } @array; print "nhap so vao "; my $so =<STDIN>; foreach (@array){
       print "$i\t$_\n";
       last if ($i++ >= $so); } } close($fh);

Thank you for help.
How can I do if I want to search all the data hoi.txt and it did not decide at n=31 beacuse I will use the script to run data (data size 87800 KB).

Again, I'm not too sure if this is what you want but try:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Not sure what this is really used for my $filename = 'hoi.txt'; open my $fh, '<', $filename or die "Failed to open $filename: $!"; my $i =1; my @array = (); # Loop thru all of the data in the file while (my $rec = <$fh>){ next if $rec =~ m/^#/; #Skip comment lines $rec =~ s/\s*$//; #Remove spaces or newline characters from end push @array, $rec; } close($fh); # Sort all of the data that was in the file @array = sort { my ($sa, $sa1) = split /\s+/, $a;
        my ($sb,$sb1) = split /\s+/, $b;
        $sb <=> $sa;
        } @array; print "nhap so vao "; my $so =<STDIN>; foreach (@array){ print "$i\t$_\n"; last if ($i++ >= $so); }

Again, I'm not too sure if this is what you want but try:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Not sure what this is really used for my $filename = 'hoi.txt'; open my $fh, '<', $filename or die "Failed to open $filename: $!"; my $i =1; my @array = (); # Loop thru all of the data in the file while (my $rec = <$fh>){ next if $rec =~ m/^#/; #Skip comment lines $rec =~ s/\s*$//; #Remove spaces or newline characters from end push @array, $rec; } close($fh); # Sort all of the data that was in the file @array = sort { my ($sa, $sa1) = split /\s+/, $a;
        my ($sb,$sb1) = split /\s+/, $b;
        $sb <=> $sa;
        } @array; print "nhap so vao "; my $so =<STDIN>; foreach (@array){ print "$i\t$_\n"; last if ($i++ >= $so); }

Thanks a lot histrungalot!
It run well. your help show me know what wrong in my script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.