Subject: | results oddities |
Hi,
I need to extract nested data. They have many attributes besides their
values, sometimes, but I'm mostly iterested in the values and the
structure.
parameter1= { name=> ... ,
value=>[ ... ],
attr1 => ...,
attr2 => ...,
... ,
}
I would like to see as a result is
%/ = {
set1 => { parameter1 => [value1 , ... ],
parameter2 => [value2, ... ],
subset1 => { parameter3 => [value3, ...],
...
},
set2 => { ...
},
... ,
}
}
I have several issues with the results, though.
1. As I understand, there is no simple way to change the key in the
parent hash of a rule from <rule: rule1> to i.e. $MATCH{name}.
2. Do a subrule call whose name is evaluated at runtime. I.e. when the
pattern matches String, call <String>, if it matche Long, call <Long>.
In other words is there something evaluate subrule then use that value
as name of another subrule call
<rule: var> # "number"=int { 2 } "title"=string { "abc" } "float"=double
{ 2.578 3.0 5.0 6.3 7.1 10.0 }
<name=string> = <type=(\w+) \{ <[value=eval (<\type> )]>? \}
<token: int> [+-]?\d+
<token: string> \".*\"
<token: double> <int>(\.\d+)?
3. In the sample code attached, I tried to experiment a little but am
not really satisfied. How can I change the 'pro' key to the value of
'tab' (s. 1.)? The only solution appears to wrap it into another rule
and assign the value matches to the name matches, right? I'd like to see
a simpler solution like a <key: > directive.
Subject: | testgr.pl |
#!/usr/bin/perl
#
use v5.10;
use warnings;
use strict;
package Simple_Test;
use Data::Dumper;
sub new {
my ($class)=@_;
return bless $class;
}
sub row {
my ($self,$res)=@_;
my %h;
say Dumper $res;
$h{$res->{count}}=(($res->{arg}));
return \%h;
}
sub tab {
my ($self,$res)=@_;
say Dumper $res;
my %r;
my @t;
#say Dumper $res;
if ($res->{tab}) {
for my $row (@{%{$res}->{row}}) {
say "Row ",Dumper $row;
push @t,%{$row};
}
%r=@t;
say "Array @t - hash ",Dumper (%r);
return bless \%r;
} else {
$r{$res->{not}}=$res->{sc};
say Dumper $res;
return bless \%r;
}
}
1;
my $gr=do {
use Regexp::Grammars;
qr{
# <debug: on>
<nocontext:>
<all>
#<pro>
<rule: all> <MATCH=pro>
<token: int> [+-]?\d++
<token: literal> [.'!%\\\w_\^@\+\-\(\)\(\)\#{}\\\[\]]++ <minimize:>
<objrule: pro> (<?literal> <tab=(\w+Table)> <[row]>+ % <ws>
|<not=literal> <sc=int>?)
#<rule: tab>
#(?=<literal>) \w+Table
<objrule: row> <count=int> (<arg=literal>)?
# (?{$ARG{tab}->{tab}=>{};$ARG{tab}->{tab}->{$MATCH{int}}=>$MATCH{literal};})
#(?{$MATCH{eval ($MATCH{int})}=>($MATCH{literal}); })
}xms;
};
while (my $line = <>) {
if ($line =~ $gr->with_actions('Simple_Test')) {
use Data::Dumper 'Dumper';
say "Result: ",Dumper \%/;
}
}