in reply to Re^2: Writing to File based on condition
in thread Writing to File based on condition
For the above could I have said the following to put the dead ips into the hash ? Is there a difference?
Yes. There may be hosts in your down file which at that time were down, but at the current run are up again.
Also I am not understanding your logic on this line, all ips dead and alive are now in keys %down, you are assigning the value of 1 unless the ip is not apart of keys %down?I am assigning the value 1 to the key in %down unless the host is not down, which implies it even exists in the hash %down and its value is not 0. So, hosts which aren't present anymore in the hosts file, but in the down file, are skipped. Then in the next line I delete the host if present in %down if it happens to be up.
then lastly, couldn't I just have said below, or you had to go through the hash to get \n formatting?
The below is wrong because it uses $ifh, which is input filehandle here, and because only a bare print implies $_, while print to an explicit filehandle does not. You have to mention $_. To get a newline at the end of each print operation, you have to mention that as well, unless you set $\ (output record separator, see perlvar)
@list = (a..z); { # ok for STDOUT local $\ = $/; # set output record sep to input record sep for this +block print for @list; } { # wrong open my $fh,'>', 'foo' or die "Oops: foo - $!\n"; local $\ = $/; print $fh for @list; # WRONG. This prints GLOB(0x1945bd8) or such 26 + times } # file 'foo' closed here, since $fh is out of scope { # ok open my $fh,'>', 'bar' or die "Oops: bar - $!\n"; my $fh = select $fh; # $fh now default filehandle local $\ = $/; print for @list; # OK select $fh; # restore STDOUT as default filehandle } # file 'bar' closed here, since $fh is out of scope
Running the above, you will have a..z printed to STDOUT, each character on a line, a file "foo" with 0 bytes and a file "bar" with 52 bytes.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Writing to File based on condition
by cbtshare (Monk) on Feb 18, 2018 at 18:05 UTC | |
by shmem (Chancellor) on Feb 18, 2018 at 18:44 UTC |