Help with printing sorted expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with printing sorted expression
# 1  
Old 04-27-2011
Lightbulb Help with printing sorted expression

Hi All,
How can I print the sorted results of the following expression in Perl ??
Code:
print "$i\t$h{$i}\n";

I tried
Code:
print (sort ("$i\t$h{$i}")"\n");

and other variations of the same but failed.
Can someone suggest how to solve this problem, as I'm tryin print sorted results of my script, which otherwise works fine.
Thanks in advance.
CheersSmilie

Last edited by pawannoel; 04-27-2011 at 12:46 PM.. Reason: pressed Enter accidently before completion of post
# 2  
Old 04-27-2011
Post sample data contained in that hash and how it should look like after sorting.
# 3  
Old 04-27-2011
Sure
sample data in the hash
Code:
SK1.scplasm1    1
SK1.chr06    1
SK1.chrm    2
SK1.chr08    2
SK1.chr07    2

expected output after sort
Code:
SK1.chr06    1
SK1.chr07    2
SK1.chr08    2
SK1.chrm    2
SK1.scplasm1    1

Cheers Smilie
# 4  
Old 04-27-2011
Try:
Code:
@sorted=sort {$a cmp $b} keys %h;
for $i (@sorted){
  print "$i\t$h{$i}\n";
}

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 04-27-2011
Got that in the loop ... works great !! can you please explain how $a cmp $b works here ??
Cheers bartusSmilie
# 6  
Old 04-27-2011
"cmp" is a similar operator to "<=>", just designed to compare strings Smilie
This User Gave Thanks to bartus11 For This Post:
# 7  
Old 04-28-2011
Bug

@Bartus
I have 2 questions about a script relating to the parts in red
1. How can I combine the two IF statements in the WHILE loop so that I get output only if those conditions are met.
2. How so I print sorted results of hash of hashes in the END part
Script
Code:
#!/usr/bin/perl 
print "Enter minimum coverage: ";
chomp($min_cov=<STDIN>);
print "Enter minimum novel_Allele_Starts: ";
chomp($min_nas=<STDIN>);
open I, "$ARGV[0]";
while (<I>){
         @F=split;     
  $F[8]=~/coverage=([^;]+)/;
  if ($1>=$min_cov){
  $cov{$F[0]}{$F[3]}+=$1;
  $F[8]=~/novelAlleleStarts=([^;]+)/;
  if ($1>=$min_nas){
  $nas{$F[0]}{$F[3]}+=$1;
  }
  $n{$F[0]}{$F[3]}++;
  $h{$F[0]}++;
  }
}
END{
    for $i (keys %cov){
    for $j (keys %{$cov{$i}}){
      print "$i\t$j\t";
      printf "%.1f\t", ($cov{$i}{$j}/$n{$i}{$j});
      printf "%.1f\n", ($nas{$i}{$j}/$n{$i}{$j});   
    }
  }
  for $i (keys %h){
      @sorted=sort {$a cmp $b} keys %h;
  }
for $i (@sorted){
  print "$i\t$h{$i}\n";
        $t+=$h{$i};
 }
}

sample file for running script
Code:
SK1.chr10    SOLiD_diBayes    SNP    3120    3120    1.000000    .    .    genotype=A;reference=G;coverage=179;refAlleleCounts=32;refAlleleStarts=21;refAlleleMeanQV=15;novelAlleleCounts=117;novelAlleleStarts=27;novelAlleleMeanQV=17;diColor1=23;diColor2=23;het=0;flag=h8, 
SK1.chr01    SOLiD_diBayes    SNP    8621    8621    0.000000    .    .    genotype=C;reference=T;coverage=198;refAlleleCounts=5;refAlleleStarts=5;refAlleleMeanQV=18;novelAlleleCounts=150;novelAlleleStarts=49;novelAlleleMeanQV=18;diColor1=21;diColor2=21;het=0;flag=h4,h10,h9, 
SK1.chrm    SOLiD_diBayes    SNP    8844    8844    1.000000    .    .    genotype=C;reference=A;coverage=205;refAlleleCounts=38;refAlleleStarts=21;refAlleleMeanQV=24;novelAlleleCounts=164;novelAlleleStarts=51;novelAlleleMeanQV=27;diColor1=01;diColor2=01;het=0;flag=h8, 
SK1.chr07    SOLiD_diBayes    SNP    8852    8852    1.000000    .    .    genotype=T;reference=C;coverage=161;refAlleleCounts=31;refAlleleStarts=16;refAlleleMeanQV=24;novelAlleleCounts=127;novelAlleleStarts=47;novelAlleleMeanQV=23;diColor1=11;diColor2=11;het=0;flag=h8, 
SK1.chr07    SOLiD_diBayes    SNP    8865    8865    0.999102    .    .    genotype=G;reference=A;coverage=74;refAlleleCounts=11;refAlleleStarts=7;refAlleleMeanQV=26;novelAlleleCounts=59;novelAlleleStarts=29;novelAlleleMeanQV=24;diColor1=12;diColor2=12;het=0;flag=h8,

I'm not able to figure it out, can you please enlighten on this?
Cheers and have a nice day Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. UNIX for Dummies Questions & Answers

Help with printing sorted array of numbers

Dear All, I am trying to sort an array of numbers to retrieve the mimimum and maximum values of numbers in that array, by printing the first and last elements of the sorted array. My code is @sorted_numbers = sort (@numbers); print "@sorted_numbers\n"; print "$sorted_numbers,... (0 Replies)
Discussion started by: pawannoel
0 Replies

4. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

5. Shell Programming and Scripting

Printing several lines of a file after a specific expression

Hello, I want to print a number of lines of a file after a specific expression of a line. I have this sed command but it prints only 1 line after the expression. How could I adapt it to print for instance 10 lines after or 15 lines after ? sed -n '/regexp/{n;p;}' Thx & Regs, Rany. (5 Replies)
Discussion started by: rany1
5 Replies

6. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

7. UNIX for Advanced & Expert Users

Sorted file

Hi Is there any unix shell command or utility to know if the file is sorted or not? Thanks (3 Replies)
Discussion started by: ksailesh
3 Replies

8. Shell Programming and Scripting

SED printing just parts of lines that match an expression

Hi - I am guessing this is fairly simple for someone .. but I can not quite figure it out. I need a sed command to print just parts of lines from a file. e.g. filea.txt 4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk Message log started Message log ended 4710452 : Success : MODIFY :... (7 Replies)
Discussion started by: sniper57
7 Replies

9. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

10. Shell Programming and Scripting

sorted processes

Hi, I am trying to make a script that creates a list of all active (alive) processes sorted by size and then print this list on screen. Could anyone help me? Thaks a lot (7 Replies)
Discussion started by: pro
7 Replies
Login or Register to Ask a Question