Hello Sonali,

I appreciate that you are trying to learn how to do some basics in Perl, and you want to understand how things work. But one of the very best reasons to use a CPAN module for a common task is that it has probably considered all the "edge cases" that you might encounter in your data. Reputable CPAN modules come with a test suite that demonstrates this. So the risk of writing your own solution is that you may miss a special case, and you won't have a test for it to reveal your error.

At the least you should compare the results you get with the results from another processor. Here is a solution using Config::Tiny::Ordered:

use strict; use warnings; use Config::Tiny::Ordered; my $file = '1179628.txt'; my $config = Config::Tiny::Ordered->read( $file ); foreach my $section_name( sort keys %{ $config } ) { print "SECTION: $section_name\n"; foreach my $item( @{ $config->{ $section_name } } ) { printf ( " %7s : %s \n", $item->{'key'}, $item->{'value'} ); } print "\n"; } __END__
Output:
$ perl 1179628.pl SECTION: CELL_NAME1 COMMENT : "Perl parsing" FIRST : "TEST1" SECOND : "ID1" THIRD : 123 FOURTH : "THREE" FIFTH : 12345 SIXTH : 6789 SEVENTH : QWERTY SECTION: CELL_NAME2 COMMENT : "Tester" FIRST : "TEST2" SECOND : "ID2" THIRD : 1234 FOURTH : "FOUR" FIFTH : 12345 SIXTH : BOARD SEVENTH : MOUSE SECTION: CELL_NAME3 COMMENT : "Parser" FIRST : "TEST3" SECOND : "ID3" THIRD : 12345 FOURTH : "FIVE" FIFTH : 12345 SIXTH : PAD SEVENTH : KEY
Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Parse a file and store it in hash of hashes by 1nickt
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.