in reply to Re: Writing to File based on condition
in thread Writing to File based on condition

Thank you very much, I resolved this in my next reply, let me know if we had the same idea,but for now
my %down; open my $ifh, '<', $hostfile or die "Can't read '$hostfile: $!\n"; while(my $addy = <$ifh>) { chomp $addy; $down{$addy} = system qq{nc -w 1 $addy 22 > /dev/null 2>&1}; # 0 = + up, not 0 = down }
For the above could I have said the following to put the dead ips into the hash ? Is there a difference?
@down=<$ifh>; foreach my $line(@down) { push @down{$addy}, $_ ; } or @down{$addy}=<$ifh>;
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?
$down{$addy} = 1 unless ! $down{$addy}; delete $down{$addy} if exists $down{$addy} and ! $down{$addy}; }
then lastly, couldn't I just have said below, or you had to go through the hash to get \n formatting?
print $ifh sort keys %down;

Replies are listed 'Best First'.
Re^3: Writing to File based on condition
by shmem (Chancellor) on Feb 18, 2018 at 16:17 UTC
    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'
      Thank you for the explanation, but in this example I learnt it is possible to print hash keys directly to an array without doing a for loop. I think the difference is in your example its an array you want to print to a filehandle
      if( keys %old > $oldsize ) # only write if new ips added { open my $out, '>', $awsLists or die "$! opening $awsLists"; ##print directly to the file the new keys difference print $out sort keys %old; # sort not needed, but nice touch :) close $out; }
      https://www.perlmonks.org/?node_id=1208878
        print $out sort keys %old;

        Try that. As is, this would print all keys concatenated together as one string. That's fine if each key has a trailing record separator attached.

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