in reply to altering HoH keys

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' } };