hi Monks

I have a question regarding the use of hashes and whether this approach is feasible, I need to write a small piece of code which processes a list of servers, and for each server runs and command and pulls back what components are running against that server and pattern matches just the component name from the list

Example Server A Component1 Component2 Component3 Component4 Server B Component1 Component2 Component3 Server C Component1 Component2

And so on. The code I have is below

#!/usr/bin/perl $logfile="/tmp/gatherinfo.log"; @srvlist = ('serverA','serverB','serverC'); open(LOGFILE,">>$logfile")||die ("cannot create and open file"); foreach $_ (@srvlist){ chomp $_; @cmd = `pulldata -e $_`; foreach $line (@cmd) { chomp ($line); if ($line =~ '^\s+(\w+):\s.*') { $RM=$1; <ASSIGN TO HASH AND PRINT> print LOGFILE <hash content> } } } } close(LOGFILE);

I know hash uses key=value so what im looking to do is dynamically assign server stored in default variable $_ as key into the hash and the component value ($RM) as value and print it. Is this a pragmatic approach?


In reply to Constructing hashes by Mark.Allan

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.