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

I have an array of hashes in: @recordset_TA_SITE For example: $recordset_TA_SITE[0]{'Age'} contains the age of a site, say 10. Now I want to create a more simple variable with the hash information of record 0. I did:

$site = $recordset_TA_SITE[0];

The result is that values of $site and $recordset_TA_SITE[0] are the same: HASH(0x85449b8)

Now I expect $site{'Age'} to be 10. But it's empty... Why is that?

  • Comment on Assignment of hash to hash doesn't work?

Replies are listed 'Best First'.
Re: Assignment of hash to hash doesn't work?
by rnewsham (Curate) on May 13, 2014 at 11:39 UTC

    Are you using strict and warnings? I suspect that you may not be as you should be getting an error similar to below

    Global symbol "%site" requires explicit package name at foo.pl line 10 +1. Execution of foo.pl aborted due to compilation errors.

    Your array contains references to hashes so you need to dereference them to get at the contents.

    $site->{'Age'}

    Alternatively so you can use $site{'Age'}; you can do this.

    my %site = %{$recordset_TA_SITE[0]};
Re: Assignment of hash to hash doesn't work?
by Monk::Thomas (Friar) on May 13, 2014 at 11:41 UTC

    $site is not a hash but a hash reference

    Either use

    my %site = %{ $recordset_TA_SITE[0] }; print $site{'Age'};

    or

    my $href = $recordset_TA_SITE[0]; print $href->{'Age'};
Re: Assignment of hash to hash doesn't work?
by hdb (Monsignor) on May 13, 2014 at 13:30 UTC

    In addition to what the fellow brothers said above, $recordset_TA_SITE[0]{'Age'} only works because Perl is secretly interpreting it as $recordset_TA_SITE[0]->{'Age'}. This happens only for nested structures (more precisely: from the second level onwards) and therefore $site{'Age'} is not automatically interpreted as $site->{'Age'}.

      Or more easy to remember: Perl assumes a '->' between closing and opening square and curly brackets.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
Re: Assignment of hash to hash doesn't work?
by Anonymous Monk on May 13, 2014 at 11:41 UTC

    See perlreftut and perlref - what you've actually got with @recordset_TA_SITE is an array of references to hashes. What the statement $site = $recordset_TA_SITE[0]; does is copy the hash reference from the array element to $site, and since $site is now a reference to a hash, you have to dereference it, like this: $site->{'Age'}. If you had used use warnings; use strict; at the top of your script, you would have likely gotten an error, because $site and $site{'Age'} are two entirely different variables - the latter is the hash %site.