http://qs321.pair.com?node_id=1197084

345qwerty has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have such problem: I want to print all lines with new added column: IN or OUT, the script runs, but I can't understand, why there is only first line printed...do you have any suggestions?

This is my script:

use strict; use warnings; open(FILE, "Q.txt"); my $file = <FILE>; my @column; while (<FILE>) { @column=($file); chomp @column; my $number=0; while($number <= $#column) { #go through the array from 0 to th +e last element my $j; my $count=0; foreach ($j=$number; $j < $#column; $j++) { #select the numbers + from the beginning of the line my $d=($column[$j]=~/(\d+)/)[0] - ($column[$j+1]=~/(\d+)/)[0]; #d +ifference last if abs($d)!= 1; #if differ more than 1 - lea +ve. $count+=$d; #accumulate the difference. } if(abs($count)>=8) { $column[$_].= " t\IN " for $number..$j; $number=$j+1; } if(abs($count)<8){ $column[$number].= " \tOUT" ; $number++; } } } print for @column; exit;

And here is 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 101 Q CAG 102 Q CAG 106 Q CAG 107 Q CAG

Here is my OUTPUT FILE, but here is just the 1st line...:

5 Q CAA OUT

Thank you!

2017-08-12 Athanasius added code tags around data

Replies are listed 'Best First'.
Re: How can I print all lines?
by hippo (Bishop) on Aug 09, 2017 at 09:27 UTC
    my $file = <FILE>; my @column; while (<FILE>) { @column=($file);

    You only set $file once and never change it. You could assign it as part of the while conditional:

    while (my $file = <FILE>) {

    and please, put your code inside <code> tags and your data inside other <code> tags.

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

      591 Q CAG OUT

        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.

Re: How can I print all lines?
by marto (Cardinal) on Aug 09, 2017 at 09:53 UTC

    As a side note to what hippo said, consider using the three argument open, and report failures:

    open( my $fh, "<", "Q.txt" ) or die "can't open file: $!";
Create separated column for my file
by 345qwerty (Novice) on Aug 10, 2017 at 10:47 UTC

    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

      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.

      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