Parsing Config file
am 12.06.2007 19:13:43 von Michael Higgins
Hello, Gurus --
I'm stuck finding the best approach to parsing a config file.
Some configuration items are grouped, nested, in matching braces '{}'. Some
are item = value.
I need to modify and replace three sections that are in this format
item{.....item{....}}, capturing up to the matched brace.
Any suggestions?
Cheers,
--
. . . . . .... . . . .. .. . . . .``.
.`. .`. . . .` . . . . .` .` . .`. . `.
. ` . . .`. .`` .```. . . .. . .. . . `. `.
. . . . `. .... . . . `..` `..` . . . `..`
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Parsing Config file
am 13.06.2007 03:43:47 von Bill Luebkert
Michael Higgins wrote:
> Hello, Gurus --
>
> I'm stuck finding the best approach to parsing a config file.
>
> Some configuration items are grouped, nested, in matching braces '{}'. Some
> are item = value.
>
> I need to modify and replace three sections that are in this format
> item{.....item{....}}, capturing up to the matched brace.
>
> Any suggestions?
Sample config file as currently is:
Sample config file that you want to change to instead of brace version:
Any pertinent rules in detail.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Parsing Config file
am 13.06.2007 04:26:59 von Michael Higgins
> Bill Luebkert wrote:
>
> Michael Higgins wrote:
> > Hello, Gurus --
> >
> > I'm stuck finding the best approach to parsing a config file.
> >
> > Some configuration items are grouped, nested, in matching
> braces '{}'. Some
> > are item = value.
> >
> > I need to modify and replace three sections that are in this format
> > item{.....item{....}}, capturing up to the matched brace.
> >
> > Any suggestions?
>
> Sample config file as currently is:
Like this:
DEBUG = OFF
THRESHOLD = 4
SERVER = .
USER = BATCH
PASSWORD = PASSWORD
Over Write = OVER WRITE
BATCH TASK{
TASK NAME = The Task
SLEEP TIME{
SECOND = 10
}
}
.. . .
and similar. This first '{' starts nested data structures like above. They
all match up.
>
> Sample config file that you want to change to instead of
> brace version:
>
Actually, I think I want to parse this one into a perl structure, change a
few items and write it back out mostly unchanged. Right now, I'm pulling the
bits to change with a bunch of substrs and regexen. It's ugly, but it will
work. I think I should be able to get something more elegant, like a bunch
of hash and array refs, automagically. ;-)
> Any pertinent rules in detail.
I don't know, exactly. Lines are either 'ITEM = VALUE' or begin a config
block like 'ITEM{' -- the same rules inside the block. The braces all match
up but 'ITEM = VALUE' can appear inside the blocks too.
Basically, I thought to use something like Parse::RecDescent or
Text::Balanced... or something else?
Thanks for any help or suggested approach.
Cheers,
--
. . . . . .... . . . .. .. . . . .``.
.`. .`. . . .` . . . . .` .` . .`. . `.
. ` . . .`. .`` .```. . . .. . .. . . `. `.
. . . . `. .... . . . `..` `..` . . . `..`
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Parsing Config file
am 13.06.2007 05:35:08 von Foo JH
Your config file seems to map quite well to a nested hash table.
You will probaby need 2 kinds of parsers:
1. The simple name = value pair parser, which should be easy to write
with regex
2. The nest {} parser, which you probably can use a recursive function
to parse deeply
The other (easier) way is to analyse if the .ini format will work just
as well. It will look like the following:
[Main]
DEBUG = OFF
THRESHOLD = 4
SERVER = .
USER = BATCH
PASSWORD = PASSWORD
Over Write = OVER WRITE
[BATCH TASK]
TASK NAME = The Task
SLEEP TIME SECOND = 10
This will make your parsing much much easier.
Michael Higgins wrote:
>> Bill Luebkert wrote:
>>
>> Michael Higgins wrote:
>>
>>> Hello, Gurus --
>>>
>>> I'm stuck finding the best approach to parsing a config file.
>>>
>>> Some configuration items are grouped, nested, in matching
>>>
>> braces '{}'. Some
>>
>>> are item = value.
>>>
>>> I need to modify and replace three sections that are in this format
>>> item{.....item{....}}, capturing up to the matched brace.
>>>
>>> Any suggestions?
>>>
>> Sample config file as currently is:
>>
>
> Like this:
>
> DEBUG = OFF
> THRESHOLD = 4
> SERVER = .
> USER = BATCH
> PASSWORD = PASSWORD
> Over Write = OVER WRITE
> BATCH TASK{
> TASK NAME = The Task
> SLEEP TIME{
> SECOND = 10
> }
> }
> . . .
>
> and similar. This first '{' starts nested data structures like above. They
> all match up.
>
>
>> Sample config file that you want to change to instead of
>> brace version:
>>
>>
>
> Actually, I think I want to parse this one into a perl structure, change a
> few items and write it back out mostly unchanged. Right now, I'm pulling the
> bits to change with a bunch of substrs and regexen. It's ugly, but it will
> work. I think I should be able to get something more elegant, like a bunch
> of hash and array refs, automagically. ;-)
>
>
>> Any pertinent rules in detail.
>>
>
> I don't know, exactly. Lines are either 'ITEM = VALUE' or begin a config
> block like 'ITEM{' -- the same rules inside the block. The braces all match
> up but 'ITEM = VALUE' can appear inside the blocks too.
>
> Basically, I thought to use something like Parse::RecDescent or
> Text::Balanced... or something else?
>
> Thanks for any help or suggested approach.
>
> Cheers,
>
> --
> . . . . . .... . . . .. .. . . . .``.
> .`. .`. . . .` . . . . .` .` . .`. . `.
> . ` . . .`. .`` .```. . . .. . .. . . `. `.
> . . . . `. .... . . . `..` `..` . . . `..`
>
>
>
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Parsing Config file
am 13.06.2007 09:23:14 von Ari.Wilen
Hi Michael
perhaps Config-IniFiles.pm helps. At least it is easy. =
Terveisin - Best regards =
Ari =
____________________________________ =
-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-bounce=
s@listserv.ActiveState.com] On Behalf Of Michael Higgins
Sent: 12. kesäkuuta 2007 20:14
To: activeperl@listserv.ActiveState.com
Subject: Parsing Config file
Hello, Gurus --
I'm stuck finding the best approach to parsing a config file.
Some configuration items are grouped, nested, in matching braces '{}'. Some
are item =3D value.
I need to modify and replace three sections that are in this format
item{.....item{....}}, capturing up to the matched brace.
Any suggestions? =
Cheers,
--
. . . . . .... . . . .. .. . . . .``.
.`. .`. . . .` . . . . .` .` . .`. . `.
. ` . . .`. .`` .```. . . .. . .. . . `. `.
. . . . `. .... . . . `..` `..` . . . `..`
=
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Parsing Config file
am 14.06.2007 00:15:02 von andrew fresh
On Tue, Jun 12, 2007 at 07:26:59PM -0700, Michael Higgins wrote:
> Like this:
>
> DEBUG = OFF
> THRESHOLD = 4
> SERVER = .
> USER = BATCH
> PASSWORD = PASSWORD
> Over Write = OVER WRITE
> BATCH TASK{
> TASK NAME = The Task
> SLEEP TIME{
> SECOND = 10
> }
> }
> . . .
>
> and similar. This first '{' starts nested data structures like above. They
> all match up.
If they are all on separate lines then you could do something like this:
You would have to handle blank values differently, and if there can be
more than one key with the same name the last one wins.
This was just a really quick hack so you probably want to expand on it.
#!/usr/bin/perl
use strict;
use warnings;
my $data = Parse();
use Data::Dumper;
print Dumper $data;
sub Parse
{
my %data;
while () {
chomp;
s/(^\s+)|(\s+$)//g;
my ($key, $value) = split /\s*=\s*/;
if ($value) {
$data{$key} = $value;
} elsif ($key =~ s/\s*{$//) {
$data{$key} = Parse();
} elsif (/^}$/) {
return \%data;
} else {
warn "Invalid line ($.): $_\n";
}
}
return \%data;
}
__DATA__
TEST=1
FILE=myfile
SUB{
SUBSTUFF = 12345
SUBMORE = 54321
SUBSUB {
1234 = abc
}
EMPTYSUB{
}
}
--
andrew - ICQ# 253198 - Jabber: andrew@rraz.net
BOFH excuse of the day: permission denied
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs