thank you very much, my final code and explanation is below, please let me know if I am not understanding something
use warnings;
use strict;
my $line;
my $awsLists = '/ansible/awsLists';
my @command;
@command=`aws ec2 describe-instances --filters "Name=instance-state-na
+me,Values=running" | grep PrivateDnsName | cut -d ":" -f2| cut -d '"
+' -f2 | sort -u `;
##Initializes an empty hash
my %old;
##Checks if the file is there to be opened and read into the handle $i
+n
if( open my $in, '<', $awsLists ) # OK if file not there
{
##If file is there then read contents into keys and associating a valu
+e of blank or () means new line?
## why wouldn't you do @old{<$in>} = undef; , would it be wrong?
@old{ <$in> } = (); # note: keys have \n
close $in;
}
###This takes the keys from the the hash previously populate from the
+file and puts it into scalar
my $oldsize = keys %old;
## Populates the hash again, but this time with the values of the @com
+mand array
## Does the old keys get overwritten?(I dont think so, eg, an array
+ values dont get overwritten everytime you push new values)
@old{ @command } = (); # add new ips
#Does the compare, and if the hash keys from the <$in> and different f
+rom the hash keys of @command
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;
}
# system qq{sed -i '/^ *\$/d' $awsLists};