I have a regexp::grammar set of rules to read a parameter files, quite complex ones. I got the matching alright, but I fail in simplifying the result structure.
Instead of the rule's name, I'd much prefer to see the name field as hash key, then simplifying the whole thing to something like this (I want to keep the arrays since the values can actually be lists):
The data looks like this, elements grouped in { }, that can be arbitrarily nested. The values to the tokens can be scalars or lists
...
<ParamMap."HARDWARE">
{
<ParamBool."MagnitudeImages"> { "true" "false" "true" }
<ParamString."ProtectedSize"> { "350" }
}
...
The (simplified) code looks like this:
my $g=qr{
<nocontext:>
<protocol>
<rule: protocol> <[MATCH=element]>+ <minimize:>
<rule: element> ( <MATCH=map> | <MATCH=p_str> | <MATCH=p_bool> ) #<min
+imize:>
<rule: map> \< ParamMap \. <name> > \{ <[element]>* \}
<rule: p_str> \< ParamString \. <name> > \{
( <[string]>* )?
\}
<rule: p_bool> \< ParamBool \. <name> > \{
#<debug: on>
( <[bool]>* )?
\}
<rule: string> <_qq> <[MATCH=literal]>* <_qq>
<token: literal> [.'!\w_@\+\-\(\)\(\)\#{}\\\[\]]+
<token: _qq> \"
<token: name> <_qq>\w*<_qq>
<rule: bool> <_qq><MATCH=(true|false)><_qq>
}xms;
My desired results would be something like that:
$VAR1={
'HARDWARE'=>[
'PhaseImages'=>[ true, false, true ]
'RoFOV'=>[ '230.0000000000000000', ]
]
}
...
so far I get this:
$VAR1 = {
'protocol' => [
{
'name' => '"HARDWARE"',
'element' => [
{
'bool' => [
'true',
'false',
'true'
],
name' => '"MagnitudeImages"'
},
{
'name' => '"ProtectedSize"',
'string' => [
[
'350'
]
]
}
]
}
]
};
My sincere apologies for the bad formating.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.