Near as I can tell you are hoping to create a header for each file, then populate the auto line, then populate the rest of the file. However that is a rather fragile concatenation of circumstances! There are two approaches that are likely to work better for you: modify your select so you get the host rows together (my $sth = $dbh->prepare("SELECT host, name, type, address, netmask, gateway FROM $table_name order by host");), or make a preliminary pass over the data, then create the files in a second pass. The second approach is shown first:

use strict; use warnings; my @data = ( ['app12', 'eth1', 'static', '192.168.0.5', '255.255.255.0', '192.1 +68.0.1'], ['app15', 'eth0', 'dhcp', '', '', ''], ['app15', 'eth1', 'static', '192.168.0.4', '255.255.255.0', '192.1 +68.0.1'], ['app17', 'eth1', 'static', '192.168.0.2', '255.255.255.0', '192.1 +68.0.1'], ); my %hosts; while (my @row = @{shift @data || []}) { push @{$hosts{$row[0]}}, \@row; } for my $host (keys %hosts) { genFile ($host, $hosts{$host}); } sub genFile { my ($host, $rows) = @_; my $fileName = "/tmp/interfaces.$host"; my @ifaces = map {$_->[1]} @$rows; print "--------- $fileName --------------\n"; print <<STR; auto lo iface lo inet loopback auto @ifaces STR for my $row (@$rows) { my ($hostname, $iface, $type, $address, $netmask, $gateway) = +@$row; if ($type eq "dhcp") { print "\niface $iface inet $type\n"; } else { print <<STR; iface $iface inet $type address $address netmask $netmask network 192.168.0.0 broadcast 192.168.0.255 gateway $gateway STR } } }

Prints:

--------- /tmp/interfaces.app15 -------------- auto lo iface lo inet loopback auto eth0 eth1 iface eth0 inet dhcp iface eth1 inet static address 192.168.0.4 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 --------- /tmp/interfaces.app12 -------------- auto lo iface lo inet loopback auto eth1 iface eth1 inet static address 192.168.0.5 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 --------- /tmp/interfaces.app17 -------------- auto lo iface lo inet loopback auto eth1 iface eth1 inet static address 192.168.0.2 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1

The alternate approach uses the same output sub, but alters the way the data is managed up front. This code is slightly more complicated, but will work for huge databases where collating all the data in a hash is not practical - although it's hard to imagine when that would be an issue:

my @hostRows; while ((my @row = @{shift @data || []}) || @hostRows) { if (@hostRows && (! @row || $row[0] ne $hostRows[0][0])) { genFile ($hostRows[0][0], \@hostRows); @hostRows = (); next if ! @row; } push @hostRows, \@row; }

This code replaces the main line code following the data in the first version. The files generated are the same with the same content, although they will most likely be generated in a different order.

True laziness is hard work

In reply to Re: 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.