Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I was wondering if anyone knows if its possible to use Tie::IxHash (or something similar) in such a way that all hashes retain their key order.
My situation is this:
I've got a text file that needs modifying, and it is structured remarkably similar to a hash of hashes (I just need to search and replace '()' with '{}' and '=' with '=>'). Because of the structure, creating a hash with a 'do' statement is very easy. The problem is, after I've performed the necessary changes, I'd like to output the file again in the same order. Usually when I need to preserve the key order for a hash, I use 'Tie::IxHash', but since I'm creating the hash of hashes from a file, I can't tie second level of hashes with 'Tie::IxHash'.
E.g.
Script:
Anyone have any thoughts on how to tie the second level hashes without needing to parse the file line by line and tie each hash as its created?
Thanks!
I was wondering if anyone knows if its possible to use Tie::IxHash (or something similar) in such a way that all hashes retain their key order.
My situation is this:
I've got a text file that needs modifying, and it is structured remarkably similar to a hash of hashes (I just need to search and replace '()' with '{}' and '=' with '=>'). Because of the structure, creating a hash with a 'do' statement is very easy. The problem is, after I've performed the necessary changes, I'd like to output the file again in the same order. Usually when I need to preserve the key order for a hash, I use 'Tie::IxHash', but since I'm creating the hash of hashes from a file, I can't tie second level of hashes with 'Tie::IxHash'.
E.g.
Script:
File:use Data::Dumper; use Tie::IxHash; tie %data, 'Tie::IxHash'; do "File"; $data{SomeMoreStuff}{SomeMoreData} = 'x'; print Dumper \%data;
The output looks like:%data = ( SomeStuff => { SomeData => 'a', SomeMoreData =>'b', OtherData =>'c', }, SomeMoreStuff => { SomeData =>'a', SomeMoreData => 'b', SomeExtraData => 'c', OtherData => 'd' } );
The output I want:$VAR1 = { 'SomeStuff' => { 'OtherData' => 'c', 'SomeMoreData' => 'b', 'SomeData' => 'a' }, 'SomeMoreStuff' => { 'OtherData' => 'd', 'SomeMoreData' => 'x', 'SomeExtraData' => 'c', 'SomeData' => 'a' } };
$VAR1 = { 'SomeStuff' => { 'SomeData' => 'a' 'SomeMoreData' => 'b', 'OtherData' => 'c', }, 'SomeMoreStuff' => { 'SomeData' => 'a' 'SomeMoreData' => 'x', 'SomeExtraData' => 'c', 'OtherData' => 'd', } };
Anyone have any thoughts on how to tie the second level hashes without needing to parse the file line by line and tie each hash as its created?
Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Tie a hash of hashes?
by ikegami (Patriarch) on Feb 02, 2006 at 20:47 UTC | |
by Anonymous Monk on Feb 02, 2006 at 22:19 UTC | |
Re: Tie a hash of hashes?
by japhy (Canon) on Feb 03, 2006 at 03:43 UTC | |
by Anonymous Monk on Feb 03, 2006 at 15:51 UTC | |
by japhy (Canon) on Feb 03, 2006 at 16:05 UTC | |
Re: Tie a hash of hashes?
by ikegami (Patriarch) on Feb 02, 2006 at 20:24 UTC | |
Re: Tie a hash of hashes?
by kutsu (Priest) on Feb 02, 2006 at 20:24 UTC |
Back to
Seekers of Perl Wisdom