building hash from stream based string with unknown number of lines?
building hash from stream based string with unknown number of lines?
am 04.02.2005 19:14:45 von Eric Teuber
Hi,
trying to store a configuration file, such as the tnsnames.ora for
Oracle databases in a hash.
Sample:
net_service_name=
(DESCRIPTION=
(ADDRESS=(protocol_address_information))
(CONNECT_DATA=
(SERVICE_NAME=service_name))
)
SOME_OPTION=some_value
The hash structure should be like this.
'DESCRIPTION' => {
'CONNECT_DATA' => {
'SERVICE_NAME' => 'service_name'
}
'ADDRESS' => 'protocol_address_information'
}
'SOME_OPTION' => 'some_value'
The Problem... i do not know how many levels of brace pairs (recursive)
exist. I bet, there is a pattern matching for this type of problem, but
i cannot figure it out. Also i could not find anything of interest at
the web.
i thought, reading the whole file as stream and count the brace pairs
and cut out the keys and values within the braces would do it, but the
script counts 60 lines of code and is still not really good working.
Any basic approach appreciated.
Eric
Re: building hash from stream based string with unknown number of lines?
am 04.02.2005 22:50:33 von Jim Gibson
In article , Eric Teuber
wrote:
> Hi,
>
> trying to store a configuration file, such as the tnsnames.ora for
> Oracle databases in a hash.
>
[text with nested, balanced parentheses snipped]
> The Problem... i do not know how many levels of brace pairs (recursive)
> exist. I bet, there is a pattern matching for this type of problem, but
> i cannot figure it out. Also i could not find anything of interest at
> the web.
> i thought, reading the whole file as stream and count the brace pairs
> and cut out the keys and values within the braces would do it, but the
> script counts 60 lines of code and is still not really good working.
Check out Text::Balanced from CPAN.
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Re: building hash from stream based string with unknown number of lines?
am 10.02.2005 16:15:32 von George Bouras
# try this
{local$/=undef;$_=}
s/[\n\r]//g;
s/\s*([\(\)=]+)\s*/$1/g;
s/\(([^=]+)\)/$1/g;
s/\)\((.+)\)/\)$1/g;
s/\(/{/g;
s/\)/}/g;
s/=/=>/g;
s/}/},/g;
eval "our \%hash=($_)";
print $hash{'net_service_name'}{'CONNECT_DATA'}{'SERVICE_NAME'} ,"\n" ;
print $hash{'net_service_name'}{'DESCRIPTION'}{'ADDRESS'};
__DATA__
net_service_name=
(DESCRIPTION=
(ADDRESS=(protocol_address_information))
(CONNECT_DATA=
(SERVICE_NAME=service_name))
)
SOME_OPTION=some_value
Re: building hash from stream based string with unknown number oflines?
am 10.02.2005 21:20:25 von Eric Teuber
George Bouras wrote:
> # try this
>
> {local$/=undef;$_=}
> s/[\n\r]//g;
> s/\s*([\(\)=]+)\s*/$1/g;
> s/\(([^=]+)\)/$1/g;
> s/\)\((.+)\)/\)$1/g;
> s/\(/{/g;
> s/\)/}/g;
> s/=/=>/g;
> s/}/},/g;
>
> eval "our \%hash=($_)";
> print $hash{'net_service_name'}{'CONNECT_DATA'}{'SERVICE_NAME'} ,"\n" ;
> print $hash{'net_service_name'}{'DESCRIPTION'}{'ADDRESS'};
>
>
>
>
> __DATA__
>
>
>
>
> net_service_name=
> (DESCRIPTION=
> (ADDRESS=(protocol_address_information))
> (CONNECT_DATA=
> (SERVICE_NAME=service_name))
> )
> SOME_OPTION=some_value
>
>
Thanks George,
and Sorry, 'cause my post was not quite right. i posted
>> The hash structure should be like this.
>>
>> 'DESCRIPTION' => {
>> 'CONNECT_DATA' => {
>> 'SERVICE_NAME' => 'service_name'
>> }
>> 'ADDRESS' => 'protocol_address_information'
>> }
>> 'SOME_OPTION' => 'some_value'
but as you already noticed the structure should be like this:
'net_service_name' => {
'DESCRIPTION' => {
'CONNECT_DATA' => {
'SERVICE_NAME' => 'service_name'
}
'ADDRESS' => 'protocol_address_information'
}
}
'SOME_OPTION' => 'some_value'
Your solution is working very well for the first key "net_service_name"
but not for the second "SOME_OPTION".
Another problem could be a random order of comments in the configuration
file like this:
net_service_name=
(DESCRIPTION=
(ADDRESS=(protocol_address_information)) # information
(CONNECT_DATA= # nonsense info
(SERVICE_NAME=service_name))
)
# something more
SOME_OPTION=some_value
.... aso.
Surely i could just throw away these comments, but that would not be a
gentle way.
Background of the problem is to find a procedure to read these
configurations of more than 100 systems, modify them and write them back
to the file automatically, if one or more of these settings change at
the ldap server. Also all configurations look different, but may have
same entries.
However, You gave me a new inspiration. I did not think of a way like
that. I will work on a final solution and come back later to the thread
with or -hopefully not- without a solution.
Thanks again,
Eric