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.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re^3: Writing to File based on condition by shmem
in thread Writing to File based on condition by cbtshare

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.