Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Parsing a file with parentheses to build a hash

by choroba (Cardinal)
on Nov 14, 2014 at 09:57 UTC ( [id://1107188]=note: print w/replies, xml ) Need Help??


in reply to Parsing a file with parentheses to build a hash

You can use the Marpa parser:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Marpa::R2; my $g = << '__G__'; lexeme default = latm => 1 :start ::= Hash :default ::= action => itself Hash ::= '(' Pairs ')' action => hash Pairs ::= Pair+ action => pairs Pair ::= '(' Key Value ')' action => pair Key ::= String Value ::= String | Pairs String ~ [^\s()]+ whitespace ~ [\s]+ :discard ~ whitespace __G__ my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$g }); my $input = '( (SECTION (Section_KEY Value1) (Another_KEY1 Value2) (KEY2 value3) (KEY3 Value4)) (NEW_SECTION (SUB_SECTION (KEY4 Value5)) (NEW_SUB_SECTION (KEY5 Value6) ) ) )'; my $recce = 'Marpa::R2::Scanless::R'->new({ grammar => $gram +mar, semantics_package => 'main +', }); $recce->read(\$input); print Dumper $recce->value; sub hash { $_[2] } sub pairs { shift; +{ map @$_, @_ } } sub pair { [ @_[2, 3] ] } sub itself { $_[1] }

Output:

$VAR1 = \{ 'SECTION' => { 'Section_KEY' => 'Value1', 'KEY2' => 'value3', 'Another_KEY1' => 'Value2', 'KEY3' => 'Value4' }, 'NEW_SECTION' => { 'NEW_SUB_SECTION' => { 'KEY5' => 'Value +6' }, 'SUB_SECTION' => { 'KEY4' => 'Value5' } } };
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Parsing a file with parentheses to build a hash
by xcellsior (Novice) on Nov 14, 2014 at 16:40 UTC
    I can see you have used this module more than once. It looks like it uses the Parse::RecDescent module. I'm going to read up on this one now. I've started to poke into the file structure now and see that it's not as cut and dry as I expected... I started to remember this as I reviewed my old code... ugggly. Anyway, I realized that I need to set up some matches for different keys because they actually have implied vars based on the key name. I'm guessing the module can handel this. I really want to learn this methodology since it seems much quicker to setup once I get my head wrapped around it. I'm going to drop a bit more of a real example.
      It looks like it uses the Parse::RecDescent module.
      No, it doesn't. It's an alternative to it.

      Also, you should consider using the <readmore> tag for the large data sample.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2025-06-20 17:24 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.