Hi there,

This code is a bit convoluted.

while (my @X=<CFGFILE>) { foreach $_(@X) { next unless /^\w/i; push (@server, $_); ($machine, $default, $type, $connect)=split(/,/,$_); print "$machine, $default, $type, $connect"; } }
@X=<CFGFILE> will slurp the file into @X in one go so your while is not looping. I would suggest something like this instead:
while (<CFGFILE>) { # read each line in turn to $_ next unless /^\w/i; push (@server, $_); # do you realy want this array too ? ($machine, $default, $type, $connect)=split(/,/,$_); print "$machine, $default, $type, $connect"; } }

Depending on what you do with the data after you have read it you may find something like a hash of arrays or hash of hashes a nice way to store it

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %servers_HoA; my %servers_HoH; while (<DATA>) { # read each line in turn to $_ next unless /^\w/i; my ($machine, $default, $type, $connect)=split(/,/,$_); print "$machine, $default, $type, $connect"; $servers_HoA{$machine}=[$default, $type, $connect]; # or perhaps $servers_HoH{$machine}{default} = $default; $servers_HoH{$machine}{type} = $type; $servers_HoH{$machine}{connect} = $connect; } print "Hash of Arrays\n"; print Dumper(\%servers_HoA); print "\nHash of Hashes\n"; print Dumper(\%servers_HoH); __DATA__ printserver1,default,cups,cups printserver2,no,aix,rsh
And here is the output of that last bit of code
printserver1, default, cups, cups printserver2, no, aix, rsh Hash of Arrays $VAR1 = { 'printserver1' => [ 'default', 'cups', 'cups ' ], 'printserver2' => [ 'no', 'aix', 'rsh ' ] }; Hash of Hashes $VAR1 = { 'printserver1' => { 'type' => 'cups', 'default' => 'default', 'connect' => 'cups ' }, 'printserver2' => { 'type' => 'aix', 'default' => 'no', 'connect' => 'rsh ' } };

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: Parsing a config file by Random_Walk
in thread Parsing a config file by tcf03

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.