This is not a straightforward task. A couple of things I can think of:
- Are the primary keys numeric? And what's the expected normalized form?
- What will happen when there is a collision with the sub-hash keys during collation?
- Database updates would be trivial once the first two are resolved.
I have written a little script below to demonstrate how to do collision in the ideal world, ie., numeric primary keys, no sub-hash key collision.
use strict;
use warnings;
use Data::Dumper;
my $href = {
'1' => { 'key1' => 'value1' },
'1.0' => { 'key2' => 'value2' },
'0001' => { 'key3' => 'value3' },
'2' => { 'key1' => 'value1' },
'0002' => { 'key2' => 'value2' },
};
print Dumper($href);
# Build a cross-reference table of primary key remapping
my %key_xref = map { $_ => normalize_key($_) } keys %$href;
# Remap primary keys / merge data
while (my ($from, $to) = each %key_xref) {
next if $from eq $to;
foreach (keys %{$href->{$from}}) {
$href->{$to}{$_} = $href->{$from}{$_};
}
delete $href->{$from};
}
print Dumper($href);
# Normalize primary keys
sub normalize_key
{
my $key = shift;
return int $key;
}
And the output -
# before
$VAR1 = {
'0002' => {
'key2' => 'value2'
},
'0001' => {
'key3' => 'value3'
},
'1' => {
'key1' => 'value1'
},
'1.0' => {
'key2' => 'value2'
},
'2' => {
'key1' => 'value1'
}
};
# after
$VAR1 = {
'1' => {
'key2' => 'value2',
'key1' => 'value1',
'key3' => 'value3'
},
'2' => {
'key2' => 'value2',
'key1' => 'value1'
}
};
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.