Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^5: Parse a file and store it in hash of hashes

by Discipulus (Canon)
on Jan 16, 2017 at 10:06 UTC ( [id://1179651]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Parse a file and store it in hash of hashes
in thread Parse a file and store it in hash of hashes

The answer is already there

If you read and apply my code you'll got:

( "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, }, "CELL_NAME1", { COMMENT => "\"Perl parsing\"", FIFTH => 12345, FIRST => "\"TEST1\"", FOURTH => "\"RANDOM\"", SECOND => "\"ID1\"", 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.

Replies are listed 'Best First'.
Re^6: Parse a file and store it in hash of hashes
by Sonali (Novice) on Jan 16, 2017 at 11:45 UTC

    Thank you. I was able to parse and dump the data into a file. Is there any function to sort the cell names after they are dumped? Because everything is dumped in a haphazard manner.

      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

        $key       => { map { split m{\s*=\s*} } @record }

        Try that with values containing "=". Like this:

        [CELL_NAME1] COMMENT = "Perl parsing = oh no" 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 = bad" FIFTH = 12345 SIXTH = BOARD SEVENTH = MOUSE [CELL_NAME3] COMMENT = "Parser" FIRST = "TEST3" SECOND = "ID3 = and how = did this = happen?" THIRD = 12345 FOURTH = "FIVE" FIFTH = 12345 SIXTH = PAD SEVENTH = KEY EOF

        Did you expect that?

        >perl 1179657.pl Odd number of elements in anonymous hash at 1179657.pl line 45, <$inFH +> chunk 3. Odd number of elements in anonymous hash at 1179657.pl line 45, <$inFH +> chunk 3. Odd number of elements in anonymous hash at 1179657.pl line 45, <$inFH +> chunk 3. %HoH = ( 'CELL_NAME1' => { '"ID1"' => 'THIRD', '"TEST1"' => 'SECOND', '"THREE"' => 'FIFTH', '123' => 'FOURTH', '12345' => 'SIXTH', '6789' => 'SEVENTH', 'COMMENT' => '"Perl parsing', 'QWERTY' => undef, 'oh no"' => 'FIRST' }, 'CELL_NAME2' => { '12345' => 'SIXTH', 'BOARD' => 'SEVENTH', 'COMMENT' => '"Tester"', 'FIRST' => '"TEST2"', 'FOURTH' => '"FOUR', 'MOUSE' => undef, 'SECOND' => '"ID2"', 'THIRD' => '1234', 'bad"' => 'FIFTH' }, 'CELL_NAME3' => { '"FIVE"' => 'FIFTH', '12345' => 'SIXTH', 'COMMENT' => '"Parser"', 'FIRST' => '"TEST3"', 'KEY' => undef, 'PAD' => 'SEVENTH', 'SECOND' => '"ID3', 'and how' => 'did this', 'happen?"' => 'THIRD' } ); >

        Hint: split accepts THREE arguments.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      If you mean by "dump the data" that you are using Data::Dumper, have a look at $Data::Dumper::Sortkeys.

      If not, maybe you can show us what you mean by "dump" and "haphazard manner".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-29 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found