tcf03 has asked for the wisdom of the Perl Monks concerning the following question:
and Id like it to dump the following:<server> printserver1 default cups cups </server> <server> printserver2 no aix rsh </server>
instead it only prints the first line and exits. conceptually I know what Im doing wrong, Im just not sure how to fix it. I need it to iterate over each server as a seperate entity.printserver1,default,cups,cups printserver2,no,aix,rsh
Thanks in advance for pointing me in the right direction.#!/usr/bin/perl -w use strict; use diagnostics; #use Data::Dumper; my $config="/var/www/cgi-bin/prt.cfg"; open (CFGFILE,$config) || die "unable to open $config: $!\n"; my ($machine, $default, $type, $connect); my $object=0; my @server; while (my @CFG = <CFGFILE>) { foreach $_(@CFG) { chomp; next if /^#/; next if /^\s/; if (/<server.*?>/i ... /<\/server.*?/i) { next if /<.?server>/i; $object++; push (@server, $_); } ($machine, $default, $type, $connect) = @server; } print "$machine,$default,$type,$connect\n"; }
and the code to:printserver1,default,cups,cups printserver2,no,aix,rsh
This makes it much easer to deal with. Thanks for the suggestions.#!/usr/bin/perl -w use strict; use diagnostics; #use Data::Dumper; my $config="/var/www/cgi-bin/prt.cfg"; open (CFGFILE,$config) || die "unable to open $config: $!\n"; my ($machine, $default, $type, $connect); my (@server,@arrayref); while (my @X=<CFGFILE>) { foreach $_(@X) { next unless /^\w/i; push (@server, $_); ($machine, $default, $type, $connect)=split(/,/,$_); print "$machine, $default, $type, $connect"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing a config file
by dragonchild (Archbishop) on Mar 10, 2005 at 15:58 UTC | |
|
Re: Parsing a config file
by ikegami (Patriarch) on Mar 10, 2005 at 15:54 UTC | |
|
Re: Parsing a config file
by Random_Walk (Prior) on Mar 10, 2005 at 16:40 UTC | |
by tcf03 (Deacon) on Mar 10, 2005 at 17:09 UTC | |
by Random_Walk (Prior) on Mar 10, 2005 at 17:24 UTC | |
|
Re: Parsing a config file
by dragonchild (Archbishop) on Mar 10, 2005 at 19:03 UTC | |
|
Re: Parsing a config file
by thinker (Parson) on Mar 10, 2005 at 16:39 UTC |