This is the code i have written so far, to get the desired output.file1: >AAAT3R length=110 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 4 +0 40 40 40 40 40 40 40 40 40 40 40 40 38 38 38 38 40 40 39 40 40 40 4 +0 40 40 40 40 40 40 38 38 37 39 36 36 40 36 35 35 35 38 40 35 35 33 3 +5 35 35 40 40 40 40 37 37 38 38 38 40 40 40 40 40 40 40 40 40 40 40 4 +0 40 40 40 40 37 36 36 31 22 22 22 20 20 20 20 20 14 >AAA2OJ length=70 18 18 18 21 35 35 35 32 32 32 33 35 38 39 37 37 39 39 39 39 39 40 40 3 +9 39 39 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 39 3 +9 37 35 35 39 37 37 37 37 37 37 37 37 37 37 33 32 32 30 20 17 17 17 0 file2: >AAAT3R_left length=6 TACATA >AAAT3R_right length=62 ACTACTGATTTGATTATCTTTGATCTCTGTCGAACTAACTATATCTTAGTATGATCTTTAAT >AAA2OJ_left length=14 TTTTGGACTATCTG >AAA2OJ_right length=14 AGGCTGTTCTTTTN result file:(expected) >AAAT3R_left length=6 40 40 40 40 40 40 >AAAT3R_right length=62 40 40 40 38 38 37 39 36 36 40 36 35 35 35 38 40 35 35 33 35 35 35 40 4 +0 40 40 37 37 38 38 38 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 4 +0 37 36 36 31 22 22 22 20 20 20 20 20 14 >AAA2OJ_left length=14 18 18 18 21 35 35 35 32 32 32 33 35 38 39 >AAA2OJ_right length=14 37 37 37 37 37 33 32 32 30 20 17 17 17 0
My problem here is, i am not able to print the unique header ID for every line. I know that the $string is a scalar variable and outside the loop, it will hold the last value. But dont know how to solve the problem. moreover, Want the results sorted according to the input file order. (as given in the expected result file). Please help me solve it. Thank u :)#!/usr/bin/perl use strict; #processing file2 open(FH1,"file2.txt") or die "cant open"; my @ar=<FH1>; my $left;my $right;my @final; foreach my $aray(@ar){ if($aray=~m/left/){ if($aray=~m/(^>.*)_\w+\s\w+(=)(\d+)/){ $left=$3; } } else{ my $right_header=$aray; if($aray=~m/(^>.*)_\w+\s\w+(=)(\d+)/){ $right=$3; push(@final,$1); push(@final,"left=$left".".."."right=$right"); } } } my %hash=@final; my $val;my $head; my ($leftnew,$rightnew);my ($side1,$num1);my ($side2,$num2); my @numright;my @numleft;my @string;my @string1; #comparing with file 1 open(FH2,"file1.txt") or die "cant open"; my @ar2=<FH2>; my $aray2; while (my ($key,$value)=each %hash){ foreach $aray2(@ar2){ if ($aray2=~m/^(>\w+)/){ $head=$1; } $val=$hash{$key}; ($leftnew,$rightnew)=split(/\../,$val); ($side1,$num1)=split(/=/,$leftnew); ($side2,$num2)=split(/=/,$rightnew); @numleft=split(' ',$aray2); @numright=reverse(@numleft); if ($head eq $key){ if($aray2=~m/^\d+/){ my $i;my $j; for($i=0;$i<scalar(@numleft);$i++){ if($i==$num1){ last; }else{ push(@string1,$numleft[$i]); } } for ($j=0;$j<scalar(@numright);$j++){ if($j==$num2){ last; } else{ push(@string,$numright[$j]." "); } } } } } print "\n",$head.'_'.'left '.'length='.$num1,"\n"; print "@string1\n"; print "\n",$head.'_'.'right '.'length='.$num2,"\n"; print reverse(@string),"\n"; $key=();$value=();$aray2=();@string=();@string1=(); }
In reply to sorting and other problems in hash by sugar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |