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":



  • 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.