Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: String searching in file

by cbtshare (Scribe)
on Feb 10, 2018 at 06:03 UTC ( #1208878=note: print w/replies, xml ) Need Help??


in reply to Re: String searching in file
in thread String searching in file

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};

Replies are listed 'Best First'.
Re^3: String searching in file
by poj (Abbot) on Feb 10, 2018 at 07:51 UTC

    ###This takes counts the keys from the the in the hash previously populate from the file and puts it into scalar

    my $oldsize = keys %old;

    #Does the compare, and if the count of hash keys from the <$in> and different from the hash keys of @command has increased (i.e. new keys added)

    if( keys %old > $oldsize ) # only write if new ips added

    ## If file is there then read contents into keys and associating a value of blank or () means new line?
    ## why wouldn't you do @old{<$in>} = undef; , would it be wrong?

      @old{ <$in> } = (); # note: keys have \n

    In this case no difference but possibly misleading you to think @old{<$in>} = 1; would assign the value 1 to all the keys from <$in> which is not the case. Try this simple example

    #!/usr/bin/perl use strict; use Data::Dumper; my @keys = ('a','b','c'); my %hash=(); @hash{@keys} = 1; print Dumper \%hash; __END__ $VAR1 = { 'c' => undef, 'a' => 1, 'b' => undef };
    poj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2019-12-15 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?