This first thing to be aware of is than in Perl we make interesting data structures by building them out of either an array or a hash using references to nested arrays or hashes as the values. So your first port of call is to References quick reference to help figure out the syntax for using references. Come back here when you have digested that.

Ok, now you should be able to figure out that @{$hosts{$row[0]}} dereferences a hash that stores references to arrays as its values. $row[0] is the host name and we are using that as the key to the hosts hash - seems appropriate. The push pushes a reference (that's what the \ in front of @row gives us) to @row (which contains a row of data for a particular interface on a host) into the array associated with the host name. Our %hosts hash is a hash of arrays of arrays!

To generate the individual host files we iterate over the key values in the %hosts hash and for each key (which is a host name remember) we call genFile passing the host name and a reference to an array containing all the rows we got for the host.

In genFile we use map {$_->[1]} @$rows; to generate a list of interfaces then use that in the header part of the file. The for loop then creates the entries for each interface.

True laziness is hard work

In reply to Re^3: Trying to create a linux interfaces file out of rows from a database. by GrandFather
in thread Trying to create a linux interfaces file out of rows from a database. by jduffany

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.