Tanalis has asked for the wisdom of the Perl Monks concerning the following question:

We're in the process of migrating between two different reporting systems at work, and part of this will involve converting client identifiers from 4-character strings to 7-character ones.

We're migrating incrementally, for various reasons (the systems are mission-critical, most importantly), and hence it's important that scripts and systems that already exist can interface, short-term, with the new code and systems put in.

To cope with the new client identifiers, I've created a hash that contains data for each client keyed on the new, 7-character, identifier, which was simple enough to do.

What I now need to do is find a way of having the old-style identifiers reference the new ones, so a call to, for example, $clients{ABCD} actually uses (and potentially modifies) the data in $clients{ABCDEFG}, rather than having to store the data twice and subsequently modify the data twice.

I've tried to do this using references, with the code below:

my %new_client_hash; foreach my $old_key (keys %clients) { my $new_key = $clients{$old_key}{new_sys_id}; delete $clients{$old_key}{new_sys_id}; $new_client_hash{$new_key} = $clients{$old_key}; $new_client_hash{$old_key} = \$new_client_hash{$new_key}; }
but this doesn't work - some of the new keys are "displaced" by the old keys, and some of the keys are simply left referencing an empty hash.

Does anyone know of a way to achieve this, or am I missing something obvious in the code there?

Any help would be appreciated.

Thanks,
-- Foxcub

Replies are listed 'Best First'.
Re: Multiple keys describing a single hash element
by pfaut (Priest) on Jan 20, 2003 at 16:25 UTC

    In these lines:

    $new_client_hash{$new_key} = $clients{$old_key}; $new_client_hash{$old_key} = \$new_client_hash{$new_key};

    $new_client_hash{$new_key} is already a reference so you are storing a reference to a reference to a hash. $clients{$old_key} is also a reference to a hash. If you store $clients{$old_key} in the new hash with both keys, they should both reference the same hash and your code should work with either key. Use this instead.

    $new_client_hash{$new_key} = $new_client_hash{$old_key} = $clients{ +$old_key};
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Multiple keys describing a single hash element
by jmcnamara (Monsignor) on Jan 20, 2003 at 16:43 UTC

    Here is a small working example. Have a look at the Hash of Hashes (HOH) section of the perldsc manpage for further details:
    #!/usr/bin/perl -wl use strict; # Map the old client names to the new names my %map = (ABCDEF => 'ABCD', GHIJKJ => 'GHIJ'); # Store the client data in a hash of hash refs my %clients = ( ABCDEF => {name => 'Sam', age => 30}, GHIJKJ => {name => 'Dan', age => 40}, ); # Link the old clients to the data foreach my $key (keys %clients) { $clients{$map{$key}} = $clients{$key}; } # Print the data, modify it and print it again print ${$clients{ABCDEF}}{name}, " is ", ${$clients{ABCDEF}}{age}; print ${$clients{ABCD }}{name}, " is ", ${$clients{ABCD }}{age}; ${$clients{ABCD}}{age} = 35; print ${$clients{ABCDEF}}{name}, " is ", ${$clients{ABCDEF}}{age}; print ${$clients{ABCD }}{name}, " is ", ${$clients{ABCD }}{age}; __END__ Prints: Sam is 30 Sam is 30 Sam is 35 Sam is 35

    --
    John.

Re: Multiple keys describing a single hash element
by gjb (Vicar) on Jan 20, 2003 at 16:29 UTC

    First of all, both the old key and the new key should refer to a reference. If $clients{$old_key} is a reference, there's no need to take the address in the assignment to $new_client_hash{$old_key}. On the other hand, if $clients{$old_key} is a value, both assignments to $new_client_hash should be of the address.

    I'd do something like

    my $value = $clients{$old_key}; $new_client_hash{$new_key} = \$value; $new_client_hash{$old_key} = \$value;
    to avoid confusion if $clients{$old_key} holds the value,
    $new_client_hash{$new_key} = $clients{$old_key}; $new_client_hash{$old_key} = $clients{$old_key};
    if it's a reference.

    Just my 2 cents, -gjb-