For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > February 2005 > Re: building hash from stream based string with unknown number of









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: building hash from stream based string with unknown number of
Eric Teuber

2005-02-10, 8:55 pm

George Bouras wrote:
> # try this
>
> {local$/=undef;$_=<DATA>}
> 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
[color=darkred]

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com