Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: reading the wrong input file out of 2 opened file

by Marshall (Canon)
on Jan 24, 2022 at 16:07 UTC ( [id://11140806]=note: print w/replies, xml ) Need Help??


in reply to reading the wrong input file out of 2 opened file

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

Replies are listed 'Best First'.
Re^2: reading the wrong input file out of 2 opened file
by Anonymous Monk on Jan 24, 2022 at 16:21 UTC
    The reads a line from F1 and puts it into $_

    wrong

      You are correct. This is an artifact of having the logical statement in the conditional. while (<FH>){} does assign the line to $_. However, while (<FH> and some condition){} doesn't assign the read line to $_ only the "truthiness" of whether a line was actually read or not is used (the line itself is thrown away). Ok, we have found yet another reason why this odd looking while statement won't work. while (defined (my $line =<FH>) and some condition){} is perhaps better. It is possible to assign to $_ (left hand side), but I seldom do that. This whole idea of an "and" or "&&" statement in the while condition looks dubious to me.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found