Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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};

In reply to Re^2: String searching in file by cbtshare
in thread String searching in file 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2018-11-16 00:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My code is most likely broken because:
















      Results (192 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!