Hi all,

I am trying to make the script to sort the data at the max to min at the decided position.

in put :
10	0
20	0
24	0
26	0
34	0
39	0
49	0
55	0
56	0
65	0
65	0
I hope out put file:

decided position 3   24  0
                     20  0
                     10  0
decided position 3   39  0
                     34  0
                     26  0
etc ....................
my script 
#!/usr/bin/perl;
use strict;
use warnings;
my ($a1, $b, $file,$n,$i, $x, $all, @array,@b, $so);		
ファイル名の指定
print "\n\n***********************************************************************\n";
print "********* Finding max and min of forward and reverse program  *********\n";
print "\nInsert the file(例:Biojet.txt or biojet.fasta):";
 $file = <STDIN>;



if (!open (IN,"$file")){
	print "ファイルの読み込みに失敗しました.\n";
	
}

print "insert the decided position:";
 $n = <STDIN>;


#ファイルの最後までループ
 $i = 0;
 $x = 0;
while (<IN>){
	
	
	if ($_ =~ /\#/){
		next;
	}
		
	 $all++;	
	 ($a1,$b) = split(/\t/,$_);
         $so =$a1;
	
	#改行コードを取り除く
	chomp ($b);
	
	#$iのカウントを増やす
	$i++;		

	if ($i <= $n) {
	 
        my @array = <$a1>;
        #@array = sort @array;

        @b = sort { $b <=>  $a} @array; 

  	print "$_\n";}     
    		  
}
close IN;

It just sort at one time and I can sort form max value to min value. Could you please show me how to solve this problem.
Thank you very much!

#!/usr/bin/perl;
use strict; 
use warnings; 
use Data::Dumper;
my $n = 3;#Decided position
my $filename = 'sample file.txt';
open my $fh, '<', $filename or die "Failed to open $filename: $!";

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) = split /\s+/, $a;
                    my ($sb) = split /\s+/, $b;
                    $sb <=> $sa;} @array;
    
    my $label = "decided position $n";
    foreach(@array){
        printf "%-23s%s\n", ($label, $_);
        $label = '';
    }
}

Thank you very much. It work very well.
Could you please show me more question how to make the line of that sorted value. I mean :

decided position 3   
       (line) 3       24  0
              2       20  0
              1       10  0
decided position 6  
              6       39  0
              5       34  0
              4       26  0
etc ....................

Thank you very much for help me.

#!/usr/bin/perl;
use strict; 
use warnings; 

my $n = 3;#Decided position
my $line_count = 0;
my $filename = 'sample file.txt';
open my $fh, '<', $filename or die "Failed to open $filename: $!";

LINE: while (1){
    my %hash = ();
    foreach (1 .. $n){
        my $rec = <$fh>;
        last LINE unless defined $rec;
        $rec = <$fh> while $rec =~ m/^#/;#Skip comment lines
        
        $rec =~ s/\s*$//;#Remove spaces or newline characters from end
        $hash{++$line_count} = $rec;
    }

    my @array = sort   {my ($sa) = split /\s+/, $hash{$a};
                    my ($sb) = split /\s+/, $hash{$b};
                    $sb <=> $sa;} keys(%hash);
    
    my $label = "decided position $line_count\n";
    foreach(@array){
        print $label;
        printf "%15d%10s\n", ($_, $hash{$_});
        $label = '';
    }
}

Thank d5e5 very much.
I can study a lot of from your script.

Could you show how to use the script if n = lenght of data.

Thank d5e5 very much.
I can study a lot of from your script.

Could you show how to use the script if n = lenght of data.

Sorry, I only know how to write the script for a pre-determined value of n. It looks like $n represents the number of data lines to read before sorting and I don't know how to determine the length of data before reading them.

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.