Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: How can I print all lines?

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


in reply to Re: How can I print all lines?
in thread How can I print all lines?

Thank you very much for reply! But now after changes it prints oly the last line...

591 Q CAG OUT

Replies are listed 'Best First'.
Re^3: How can I print all lines?
by hippo (Bishop) on Aug 09, 2017 at 09:41 UTC

    That's because you only print once, at the end. Move the print inside the while loop if you want to print each line as it is processed.

    Thanks for adding the <code> tags, but now your code is littered with <p> tags, etc. Can you remove those? It will make it much easier for us to read and understand. Update: thanks for sorting that.

      I changed my code a bit, but now it prints in this way:

      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

      I would like to get:

      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

      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;

        Step 1: What exactly is it you don't like? (CRLF in the middle of your print)

        Step 2: Where in the code does it do that? (Somewhere between printing the text of the line and printing the IN or OUT, conveniently, this is on the same line so you don't have to look very far.)

        Step 3: Consider what it is doing there. Add debug prints to make clear what your variables are at that point. (There's a \n before your \tIN... and the $column[$number] is the thing before the \t... print "-=)$column[$number](=-";? Well, that will prove that you've got a CRLF in there.)

        Step 4: Understand. The CRLF is in your string because every line of your file ends with a CRLF, of course!

        Step 5: Fix it. chomp was made for this.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-23 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found