dear monks, i m here with yet another problem. Hope you would guide me as you have always done !!! I am comparing 2 files, i take information of left and right values from file 2, and extract numbers at the beginning(left value) and end(right value) of every string-numbers (based on their unique ID which starts with '>') from file 1.
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
This is the code i have written so far, to get the desired output.
#!/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=(); }
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 :)

In reply to sorting and other problems in hash by sugar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.