On Jun 24, 3:31 am, [EMAIL PROTECTED] (Jeff) wrote:
> Hi all. I'm new to perl, a new programmer, and I badly need guidance. I'm
> trying to parse a config file with key/value pairs seperated by white space
> and surrounded by curly brackets. It has multiple fields that look like
> this:
>
> {
> Key  value
> Key   value
>
> }
>
> My solution has been to parse it with something simple --
>
> while ($file_contents =~ /(\w+)\s*\{([^}]*)\}/gs) {
>        push @new, $2;
>
> }
>
> foreach (@new){
>                   $_  =~ /\b(\w+)\s+(.*)\s+
>                           \b(\w+)\s+(.*)/xgs;
>
> My @next_tmp_variable = ($1, $2, etc);
>
> }
>
> -- but the config definitions contained in those curly brackets are
> different lengths. Some only have a four left hand values, while others have
> six or more. My solution isn't giving me what I really need.

It's actually very close.

The =~ is redundant since $_ is the default.

There's no need for the intermediate variables.

Are newlines significant? Or can just treat it as a list of
alternating keys and values delimited by whitespace?

> So I have two questions. First, I don't understand how to test this so that
> I parse all the values between the curly braces, regardless of how many
> items are there.

You don't need to repeat the pattern by hand - the /g will do that for
you.

> Second, and equally important, what kind of data structure
> should I put the results in? I think I need a hash of hashes

Probably a list of hashes would be the most natural.

my @LoH = map { { split } } $file_contents =~ /\{(.*?)\}/gs;

>. What I'd like
> to do is assign each left hand value as the key in a hash. Then, in each set
> there's a left 'command' where the right hand value will always be unique,
> which would be perfect for use as the name of an alias for the hash or as
> the key to a reference to a hash of that definition.

my %HoH;
while ( $file_contents =~ /\{(.*?)\}/gs ) {
        my %entry = split;
        $HoH{delete $entry{command}} = \%entry;
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to