use Data::Dumper;
use Tie::IxHash;
tie %data, 'Tie::IxHash';
do "File";
$data{SomeMoreStuff}{SomeMoreData} = 'x';
print Dumper \%data;
File:
%data = (
SomeStuff => {
SomeData => 'a',
SomeMoreData =>'b',
OtherData =>'c',
},
SomeMoreStuff => {
SomeData =>'a',
SomeMoreData => 'b',
SomeExtraData => 'c',
OtherData => 'd'
}
);
The output looks like:
$VAR1 = {
'SomeStuff' => {
'OtherData' => 'c',
'SomeMoreData' => 'b',
'SomeData' => 'a'
},
'SomeMoreStuff' => {
'OtherData' => 'd',
'SomeMoreData' => 'x',
'SomeExtraData' => 'c',
'SomeData' => 'a'
}
};
The output I want:
$VAR1 = {
'SomeStuff' => {
'SomeData' => 'a'
'SomeMoreData' => 'b',
'OtherData' => 'c',
},
'SomeMoreStuff' => {
'SomeData' => 'a'
'SomeMoreData' => 'x',
'SomeExtraData' => 'c',
'OtherData' => 'd',
}
};
Looking at your data it seems you might just want sort { $b cmp $a }, ie. reverse alphabetically. Or was this just the way you happened to type it and the order isn't guaranteed?
The problem:
You need to convert the hashes into Tie::IxHashes.
Solution 1:
You could traverse %data and convert the appropriate hashes.
Solution 2:
You could create a subclass of Tie::IxHash to override its method. When a hash reference is added to the hash, tie the referenced hash if it isn't already tied. Don't forget to untie when appropriate.
(I just need to search and replace '()' with '{}' and '=' with '=>').
Instead of replacing
( ... )
with
{ ... }
replace it with
new_ordered_hash( ... )
and define new_ordered_hash as follows:
sub new_ordered_hash {
my %ordered_hash;
tie %ordered_hash, 'Tie::IxHash';
%ordered_hash = @_;
return \%ordered_hash;
}
Untested.
Don't forget to [doc://untie|untie] the lower level hashes.
use Data::Dumper; use Tie::Autotie 'Tie::IxHash'; tie %data, 'Tie::IxHash'; do "File"; print Dumper \%data;I get the following output:
$VAR1 = {
'SomeStuff' => {},
'SomeMoreStuff' => {}
};
Which seems to support the module's description that its not going to work in this situation.
perlmonks.org content © perlmonks.org and Anonymous Monk, ikegami, japhy, kutsu
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03