Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Producing a hash of values from Parse::RecDescent...

Reply
Thread Tools

Producing a hash of values from Parse::RecDescent...

 
 
DJ Stunks
Guest
Posts: n/a
 
      04-12-2006
Hello all,

I'm taking my first crack at using Pastor Conway's Parse::RecDescent.
My grammar works (after some hacking around) but I'm not sure how to
get my information into a hash in the way I'd like it.

Here's a copy of a short but complete script which demonstrates what
I'm trying to accomplish. Below is an example of the structure I'd
like returned once the parser is finished.

I tried to use <autotree> but I've hardly used objects at all, and, in
any event, I want to end up with all the variables in an array.

Does anyone have any insight as to how to capture this data as desired?
My next step is to buy Advanced Perl Programming (2nd Ed)...

Thanks in advance,
-jp

=========== Script ===========

#!/usr/bin/perl

use strict;
use warnings;

use Parse::RecDescent;

my $grammar = q{
<autotree>

header : config_line(s?) 'OIDs(Variables):' variable_line(s?) /\z/

config_line : config_var /=/ config_value
config_var : /\w+/
config_value : /\S+/

variable_line : /Var-\d+ =/ oid <skip:'\('> variable_name
oid : /[.\d]+/
variable_name : /([^)]+)\)/
};

my $parser = Parse::RecDescent->new($grammar);

my $header = do {local $/; <DATA>};

if (my $header_obj = $parser->header($header) ) {
print "Valid Header\n";

use Data:umper;
print Dumper $header_obj;
}
else {
print "Invalid Header\n";
}

__DATA__
NodeName=XYZ_10.91.224.12
NodeAliasName=XYZ12-Headquarters
Category=Category.0.12
ConfName=Category.0.12
OIDs(Variables):
Var-1 = 1.3.6.1.4.1.1.2(IpAddress)
Var-2 = 1.3.6.1.4.1.1.16(PktsReceived)
Var-3 = 1.3.6.1.4.1.1.15(PktsRetransmitted)
Var-4 = 1.3.6.1.4.1.1.14(PktsSentSuccess)
Var-5 = 1.3.6.1.4.1.1.27(totalEgressBytes)
Var-6 = 1.3.6.1.4.1.1.22(totalForwardPktsDropped)
Var-7 = 1.3.6.1.4.1.1.26(totalIngressBytes)
Var-8 = 1.3.6.1.4.1.1.23(totalReversePktsDropped)
Var-9 = 1.3.6.1.4.1.1.25(totalEgressBytes)
Var-10 = 1.3.6.1.4.1.1.24(totalngressBytes)

=========== Desired Output ===========

$VAR1 = {
'Category' => 'Category.0.12',
'ConfName' => 'Category.0.12',
'NodeAliasName' => 'XYZ12-Headquarters',
'NodeName' => 'XYZ_10.91.224.12',
'Variables' => [
{
'name' => 'PktsReceived',
'oid' => '1.3.6.1.4.1.1.2'
},
{
'name' => 'PktsRetransmitted',
'oid' => '1.3.6.1.4.1.1.15'
},
{
'name' => 'PktsSentSuccess',
'oid' => '1.3.6.1.4.1.1.14'
},
{
'name' => 'totalEgressBytes',
'oid' => '1.3.6.1.4.1.1.27'
},
{
'name' => 'totalForwardPktsDropped',
'oid' => '1.3.6.1.4.1.1.22'
},
{
'name' => 'totalIngressBytes',
'oid' => '1.3.6.1.4.1.1.26'
},
{
'name' => 'totalReversePktsDropped',
'oid' => '1.3.6.1.4.1.1.23'
},
{
'name' => 'totalEgressBytes',
'oid' => '1.3.6.1.4.1.1.25'
},
{
'name' => 'totalngressBytes',
'oid' => '1.3.6.1.4.1.1.24'
}
]
};

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
hash.keys and hash.values Mage Ruby 14 08-15-2006 08:44 PM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM



Advertisments