Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

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

I wrote a parser for an old kicad brd file a while back that reads all the file contents into a hash so I can make what ever mods to the file that my mind sees fit. Recently, the Kicad maintainers moved to a new format that is compartmentalized with parentheses. The content is basically the same, but the format is a total change. I've just started to think about the best way to parse the file into the same hash structures so the data manipulation on the backend doesn't need to change too much. In an ideal world this is what I would like to happen:

File foo.kicad_pcb:

( (SECTION (Section_KEY Value1) (Another_KEY1 Value2) (KEY2 value3) (KEY3 Value4)) (NEW_SECTION (SUB_SECTION (KEY4 Value5)) (NEW_SUB_SECTION (KEY5 Value6) ) ) )

Perl Hash would look like this:

open ( BRD, "<foo.kicad_pcb") or die("ha ha"); while (<BRD>) { my $new_line = $_; chomp; // magic happens here where a has get filled in like this $DATA_HASH{SECTION}{Section_KEY} = Value1; $DATA_HASH{SECTION}{Another_KEY1} = Value2; $DATA_HASH{SECTION}{KEY2} = Value3; $DATA_HASH{SECTION}{KEY3} = Value4; $DATA_HASH{NEW_SECTION}{SUB_SECTION}{KEY4} = Value5; $DATA_HASH{NEW_SECTION}{NEW_SUB_SECTION}{KEY5} = Value6;

I've looked at a perl module called Text::Balanced but can't seem to make it do what I need (I think my brain stopped working about an hour ago). Anyway, I'm reaching out to see if someone might be able to point me in a better direction. It just seems like there should be an elegant way to do this with out counting parentheses and tracking every thing... aka not the way I would do it in C...


In reply to Parsing a file with parentheses to build a hash by xcellsior

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 perusing the Monastery: (2)
As of 2024-04-19 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found