You should always be using use strict; use warnings;. The blizzard of while statements and substitutions is very confusing to me.

while (<F1> && ($line_stop++ < $line_no)) { while (s/^[\ \t]//g) {} };
The reads a line from F1 and puts it into $_ which is thrown away, but if such a line was read (the read operation logically succeeded), then the statement compares $line_stop<$line_no. But $line_stop hasn't been declared yet, so this won't work. And we have the problem that the line itself is not available for processing. The second while loops until it cannot remove any more spaces or tabs at the beginning of the line. The while loops and the /g modifier is completely unnecessary. s/^\s+//; would be sufficient, but none of your lines have extra white space at the beginning of the line, so that won't work either.

Your code is so confusing, I have no idea what it is really supposed to do. But I took a wild ass guess based upon just looking at the input files. Code below which uses a tricky feature of opening a Perl variable like an input file so that I can show everything as a single runnable Perl program.

Maybe back up and explain what you are trying to do?

use strict; use warnings; my $IP_file = << 'END'; 1.0.129.197 1.0.131.49 1.0.131.74 1.0.138.143 1.0.138.154 1.0.139.72 END my $Port_file = << 'END'; 3 PORT state protocol 3 80/tcp closed http 3 443/tcp closed https 3 8080/tcp open http-proxy 5 80/tcp open http 5 443/tcp filtered https 5 8080/tcp filtered http-proxy END my $output_file; open my $IP, "<", \$IP_file or die "unable to open IP file for +reading $!"; open my $PORT, "<", \$Port_file or die "unable to open port file fo +r reading $!"; open my $OUT, ">", \$output_file or die "unable to open output file +for writing $!"; <$PORT>; # throw away first line ion port file while (defined (my $ip_line =<$IP>) and defined (my $port_line = <$POR +T>)) { chomp $ip_line; $port_line =~ s/^\d+\s+/$ip_line /; print "$port_line"; } __END__ Prints: 1.0.129.197 80/tcp closed http 1.0.131.49 443/tcp closed https 1.0.131.74 8080/tcp open http-proxy 1.0.138.143 80/tcp open http 1.0.138.154 443/tcp filtered https 1.0.139.72 8080/tcp filtered http-proxy

In reply to Re: reading the wrong input file out of 2 opened file by Marshall
in thread reading the wrong input file out of 2 opened file by perl_boy

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.