Data::Dumper's Sortkeys method may help you.

johngg@shiraz:~ > perl -Mstrict -Mwarnings -MData::Dumper -E ' open my $inFH, q{<}, \ <<EOF or die $!; [CELL_NAME1] COMMENT = "Perl parsing" FIRST = "TEST1" SECOND = "ID1" THIRD = 123 FOURTH = "THREE" FIFTH = 12345 SIXTH = 6789 SEVENTH = QWERTY [CELL_NAME2] COMMENT = "Tester" FIRST = "TEST2" SECOND = "ID2" THIRD = 1234 FOURTH = "FOUR" FIFTH = 12345 SIXTH = BOARD SEVENTH = MOUSE [CELL_NAME3] COMMENT = "Parser" FIRST = "TEST3" SECOND = "ID3" THIRD = 12345 FOURTH = "FIVE" FIFTH = 12345 SIXTH = PAD SEVENTH = KEY EOF my %HoH = do { local $/ = q{}; map { my @record = split m{\n}; my $key = shift @record; $key =~ s{[\[\]]}{}g; $key => { map { split m{\s*=\s*}, $_, 2 } @record }; } <$inFH>; }; print Data::Dumper ->new( [ \ %HoH ], [ qw{ *HoH } ] ) ->Sortkeys( 1 ) ->Dumpxs();' %HoH = ( 'CELL_NAME1' => { 'COMMENT' => '"Perl parsing"', 'FIFTH' => '12345', 'FIRST' => '"TEST1"', 'FOURTH' => '"THREE"', 'SECOND' => '"ID1"', 'SEVENTH' => 'QWERTY', 'SIXTH' => '6789', 'THIRD' => '123' }, 'CELL_NAME2' => { 'COMMENT' => '"Tester"', 'FIFTH' => '12345', 'FIRST' => '"TEST2"', 'FOURTH' => '"FOUR"', 'SECOND' => '"ID2"', 'SEVENTH' => 'MOUSE', 'SIXTH' => 'BOARD', 'THIRD' => '1234' }, 'CELL_NAME3' => { 'COMMENT' => '"Parser"', 'FIFTH' => '12345', 'FIRST' => '"TEST3"', 'FOURTH' => '"FIVE"', 'SECOND' => '"ID3"', 'SEVENTH' => 'KEY', 'SIXTH' => 'PAD', 'THIRD' => '12345' } );

Update: Amended map { split m{\s*=\s*} } @record to map { split m{\s*=\s*}, $_, 2 } @record to address afoken's point.

Cheers,

JohnGG


In reply to Re^7: Parse a file and store it in hash of hashes by johngg
in thread Parse a file and store it in hash of hashes by Sonali

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.