in reply to Parse a file and store it in hash of hashes
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:
Output: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__
Hope this helps!$ 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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Parse a file and store it in hash of hashes
by Sonali (Novice) on Jan 17, 2017 at 04:00 UTC |