I've been trying to create a config file parser to parse Cisco IOS configs and such. The final objective would be to show relevant data in contexts based on filters in a configuration file. For example, with such a config file it would display all interfaces where we've found the line "access vlan" as a child of the "interface" context and only show lines containing "speed", "duplex" and "description".

{ 'Context' => '^interface', 'Types' => [ 'Switch', ], 'Condition' => 'access vlan', 'Filter' => [ 'speed', 'duplex', 'description' ] };

So far, so good. I read the "running-config" and I index the lines depth (given that a non-empty line , not beginning with a space (\s) has a depth of 0) in an array. Then, in another read I use that index to read the data again, this time using relative position based on depth to create the "childs" of a context. Here's the function :

sub getDeep { my @data = (@_); my ($bighash,$hash); #First read foreach my $idx (0.. $#data) { my ($spaces, $content) = ($data[$idx] =~ m/^(\s*) +(.*)/); my $depth = length $spaces; $bighash->{node}{$idx}{depth} = $depth; } # Variables for the first read my $ldepth = 0; my $lcontext; my $lid; # Second read foreach my $id (0 .. $#data) { $data[$id] =~ s/^\s*//; next if ($data[$id] =~ /^!/); my $depth = $bighash->{node}{$id}{depth}; if ($depth eq 0) { push (@{$hash->{global}} , $data[$ +id]); $lcontext = $data[$id]; $lid = $id; } if (($depth gt 0) && ($id - $lid eq 1)) { push (@{$hash->{$lcontext}}, (" " +x $depth. $data[$id])); $lid = $id; } } return $hash; }

Using this sub, I can return a hash, then based on the presence of an arrayref for a given key, apply filters as explained. This works pretty well, so far very proud of this piece of code. Problem comes when I want to find childs of childs. In the example below, the childs of "given param2" would reprensent my next challenge.

interface XYZ given param1 -> child of "interface XYZ" given param2 -> child of "interface XYZ" given param2.1 -> child of "given param2" given param2.2 -> child of "given param2" given param3 -> child of "interface XYZ"

So after thinking about this for a while and failing with different approaches, my question comes in 2 separate parts :

1) Is there a better way to do this that I'm not seeing ?

2) How could I keep tagging childs of childs as the lines dig deeper and identify them properly in a data structure ?

Thank you for reading up to this line :)


In reply to Parsing by indentation by llarochelle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.