Hello,

I had a file which contained 3 columns

I wrote a code, which prints us added 4 column

But there is some mistake My INPUT FILE:

5 Q CAA

16 Q CAG

21 Q CAA

74 Q CAA

80 Q CAG

82 Q CAG

84 Q CAG

85 Q CAG

89 Q CAG

90 Q CAG

91 Q CAG

92 Q CAG

93 Q CAA

94 Q CAG

95 Q CAG

96 Q CAG

98 Q CAG

99 Q CAG

100 Q CAG

My OUTPUT FILE:

5 Q CAA OUT

16 Q CAG OUT

21 Q CAA OUT

74 Q CAA OUT

80 Q CAG OUT

82 Q CAG OUT

84 Q CAG OUT

85 Q CAG OUT

89 Q CAG IN

90 Q CAG IN

91 Q CAG IN

92 Q CAG IN

93 Q CAA IN

94 Q CAG IN

95 Q CAG IN

96 Q CAG IN

98 Q CAG OUT

99 Q CAG OUT

100 Q CAG OUT

BUT now I have somethis like this:

5 Q CAA OUT16 Q CAG OUT

21 Q CAA OUT

74 Q CAA OUT

80 Q CAG OUT

82 Q CAG OUT

84 Q CAG OUT

85 Q CAG OUT

89 Q CAG

IN

90 Q CAG

IN

91 Q CAG

IN

92 Q CAG

IN

93 Q CAA

IN

94 Q CAG

IN

95 Q CAG

IN

96 Q CAG

IN

98 Q CAG OUT

99 Q CAG OUT

100 Q CAG OUT

Here is my code:

use strict; use warnings; open(FILE, "<", "Q.txt"); my @column=(<FILE>); #get the lines from the standard input into an + array my $file; chomp $file; my $number=0; while($number <= $#column) { #go through the array from +0 to the last element my $j; my $count=0; foreach ($j=$number; $j < $#column; $j++) { #select t +he numbers from the beginning of the line in the current and next ele +ment my $d=($column[$j]=~/(\d+)/)[0] - ($column[$j+1]=~/(\d ++)/)[0]; #difference last if abs($d)!= 1; #if differ more than 1 - le +ave $count+=$d; #accumulate the difference } if(abs($count)>=7) { chomp($column[$_]); $column[$_]=$column[$_]. "\tIN\n" for $number..$j; + #IN if >8 $number=$j+1; } if (abs($count)<8) { chomp($column[$number]); $column[$number] = $column[$number]."\tOUT\n"; + #OUT if < 8 $number++; } } print for @column;

THE PART OF THE CODE WHICH MUST BE CHANGED TO REMOVE THIS MISTAKE IS:

if(abs($count)>=7) { chomp($column[$_]); $column[$_]=$column[$_]. "\tIN\n" for $number..$j; + #IN if >8 $number=$j+1; } if (abs($count)<8) { chomp($column[$number]); $column[$number] = $column[$number]."\tOUT\n"; + #OUT if < 8 $number++; }
Do you have any suggestions? Thank you!

2017-08-12 Reparented by Athanasius


In reply to Create separated column for my file by 345qwerty
in thread How can I print all lines? by 345qwerty

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.