Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Parsing bracket formatted file

by perlsherpa (Novice)
on Sep 26, 2022 at 05:56 UTC ( [id://11147130]=note: print w/replies, xml ) Need Help??


in reply to Parsing bracket formatted file

In the spirit of TIMTOWTDI, I figured the record looked similar enough to a Perl data structure to warrant some gratuitous use regular expressions. So I came up with the following:
use strict; use warnings; # parse the follow text block (as given) - not trying # to make a general solution here... my $text = do { local $/; <DATA> }; $text =~ s/^(.+)\{/qw|$1|,\n\r{/; $text =~ s/ \{/ => {/g; $text =~ s/{(.+)}/\[qw\/$1\/\]/g; $text =~ s/}/},/g; $text =~ s/]/],/g; $text =~ s/(\w+-\w+) =/'$1' =/g; $text =~ s/ (\w+) "(.+)"/ $1 => "$2",/; $text = sprintf qq{[%s]}, $text; my $record1 = eval $text; require Data::Dumper; print Data::Dumper::Dumper($record1); __DATA__ sys ecm cloud-provider /Common/aws-ec2 { description "The aws-ec2 parameters" property-template { account { } availability-zone { valid-values { a b c d } } instance-type { valid-values { t2.micro t2.small t2.medium } } region { valid-values { us-east-1 us-west-1 } } } }
Which outputs,
$VAR1 = [ 'sys', 'ecm', 'cloud-provider', '/Common/aws-ec2', { 'property-template' => { 'account' => [], 'region' => { 'valid-values' => [ + 'us-east-1', + 'us-west-1' ] }, 'instance-type' => { 'valid-value +s' => [ + 't2.micro', + 't2.small', + 't2.medium' + ] }, 'availability-zone' => { 'valid-v +alues' => [ + 'a', + 'b', + 'c', + 'd' + ] } }, 'description' => 'The aws-ec2 parameters' } ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147130]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2025-07-14 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.