Hello Sonali and welcome to the monastery and to wonderful world of Perl!

First of all follow the wise suggestions of the precise monk afoken.

That said, with the code you posted, and in particular $rec = $1 I get the error Global symbol "$rec" requires explicit package name at pm16012017.pl line 12. but is probably a typo.

In addition i think you just need a hash not a HashOfHash.

Now about your code: if next unless s/^\[(.*?)\]\s*//; is intended to skip the first line must probably be: next if s/^\[(.*?)\]\s*//;

Even with this you get errors about undefined values: Use of uninitialized value in hash element at inifile16012017.pl line 15, foreach line of data and the following datastructure:

$VAR1 = ''; $VAR2 = { 'FIFTH' => '12345', 'COMMENT' => '"Perl parsing"', 'SEVENTH' => 'QWERTY', 'FOURTH' => '"RANDOM"', 'SECOND' => '"ID"', 'FIRST' => '"TEST"', 'THIRD' => '123', 'SIXTH' => '6789' };

If you intended to have CELL_NAME as root element you need to not skip the line with it and have $rec declared outside the loop, to have it ad disposal during the loop:

my $rec; while ( <$fh> ) { if (s/^\[(.*?)\]\s*//){$rec = $1}

The resulting datastructure (dumped with Data::Dump with dd prettier method) will be:

( "CELL_NAME", { COMMENT => "\"Perl parsing\"", FIFTH => 12345, FIRST => "\"TEST\"", FOURTH => "\"RANDOM\"", SECOND => "\"ID\"", SEVENTH => "QWERTY", SIXTH => 6789, THIRD => 123, }, )

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

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