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!
|