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

Discussion in 'Perl Misc' started by DJ Stunks, Apr 12, 2006.

  1. DJ Stunks

    DJ Stunks Guest

    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::Dumper;
    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'
    }
    ]
    };
    DJ Stunks, Apr 12, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jean-gert nesselbosch

    XPath1.0-grammar compatible to Parse::RecDescent

    jean-gert nesselbosch, May 12, 2006, in forum: XML
    Replies:
    0
    Views:
    407
    jean-gert nesselbosch
    May 12, 2006
  2. ccm news
    Replies:
    0
    Views:
    3,071
    ccm news
    Jan 15, 2009
  3. Lex Williams

    Alternative to Parse::RecDescent

    Lex Williams, Aug 26, 2008, in forum: Ruby
    Replies:
    4
    Views:
    197
    James Gray
    Aug 27, 2008
  4. Jon Ericson

    Huffman coding and Parse::RecDescent

    Jon Ericson, Apr 22, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    170
    Jon Ericson
    Apr 23, 2004
  5. A. Farber
    Replies:
    1
    Views:
    116
    Anno Siegel
    May 11, 2004
Loading...

Share This Page