Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Create separated column for my file

by 345qwerty (Novice)
on Aug 10, 2017 at 10:47 UTC ( [id://1197150]=note: print w/replies, xml ) Need Help??


in reply to How can I print all lines?

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

Replies are listed 'Best First'.
Re: Create separated column for my file
by thanos1983 (Parson) on Aug 10, 2017 at 11:04 UTC

    Hello 345qwerty,

    You asked the same question here How can I print all lines?, I thought that you get your answer since you did not comment on the monks that tried to help you.

    Please do not open multiple questions, comment ask for assistance on your question so we can understand what is the problem and assist.

    Having said that, what do you mean by THE PART OF THE CODE WHICH MUST BE CHANGED TO REMOVE THIS MISTAKE IS:. What is the problem what is the expected output what you have tried, since you say remove this mistake? Did you failed? Is your code not compiling? Help us to help you, and spend some time understand your code. Today this is the problem tomorrow is going to be something bigger.

    Looking forward to your update, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Create separated column for my file
by kcott (Archbishop) on Aug 11, 2017 at 06:53 UTC

    G'day 345qwerty,

    "Do you have any suggestions?"

    If you write code that messy, you can be pretty much guaranteed to make mistakes. Choose a code layout that suits you and use it consistently. See "perlstyle - Perl style guide" for tips on how to accomplish this. Consider using "perltidy - a perl script indenter and reformatter".

    open(FILE, "<", "Q.txt");

    Use lexical filehandles instead of global, package variables. Check that I/O operations actually worked. See open and the autodie pragma.

    while($number <= $#column) { #go through the array from +0 to the last element $number++; }

    This is an inappropriate method for iterating an array. If you just want the elements:

    for my $element (@array_name) { # Do something with $element here }

    If you really need the indexes:

    for my $index (0 .. $#array_name) { # Do something with $index here }

    I might have further suggestions after you've tidied up the code.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1197150]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-24 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found