Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Couple of high level thoughts (attempted with minimal caffeination, so take with large conglomeration of NaCL and I may be whiffing on template syntax a bit):

This is almost complex enough you might want to look at using a proper parser (Parse::RecDescent, Marpa::R2) and build some sort of data structure to represent each "interface entry". Alternately maybe at least use "!\n" as your record separator and push the existing parsing logic off into its own sub (which then returns some sort of data structure . . .); doing that might help make sure you're not intermingling blocks and can (slightly more easily) start with a fresh parse state each one.

Also rather than a bunch of hardcoded print statements for something like this I'd use Template Toolkit (see also Template) and write something which takes the data produced by the parsing bits and then passes those to something which expands into the final output lines.

[% MACRO interface_metainfo( if_info ) BEGIN %] set interfaces ge-1/1/1 unit 23 description "[% if_info.description %] +" set interfaces ge-1/1/1 unit 23 encapsulation [% if_info.encapsulation + %] set interfaces ge-1/1/1 unit 23 vlan-tags [% FOR tag IN if_info.vlan_t +ags.keys %][% tag %] [% if_info.vlan_tags.$tag %][% END %] ## YADDA YADDA YADDA [% END %] [%- CLEAR -%] [% FOR if IN interface_list %] [% interface_metainfo( if ) %] [% protocol_block( if ) %] [% class_of_service_block( if ) %] [% END %]

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Cisco to Juniper - parser help by Fletch
in thread Cisco to Juniper - parser help by jamescmatt

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found