in reply to Hash of hashes assignment

Try this instead of the first statement, it makes a little more sense:

$hashOfHashes{$key} = \%hash;

In other words, assign a reference to %hash to the key value of $key in %hashOfHashes. Using { } instead of \ does the same thing, but it is not what I would call intuitive, plus it uses one more character - a cardinal sin if you are coding in Perl.

Replies are listed 'Best First'.
Re^2: Hash of hashes assignment
by Athanasius (Archbishop) on Apr 13, 2013 at 14:14 UTC
    Using { } instead of \ does the same thing

    Not exactly. Consider:

    #! perl use strict; use warnings; use Data::Dumper; my %hash = ( Fred => 'Wilma', Barney => 'Betty' ); my $ref = \%hash; my $copy = { %hash }; $hash{Homer} = 'Marge'; # Alter the original hash print "\nReference: ", Dumper($ref); print "\nCopy: ", Dumper($copy);

    Output:

    0:10 >perl 606_SoPW.pl Reference: $VAR1 = { 'Homer' => 'Marge', 'Barney' => 'Betty', 'Fred' => 'Wilma' }; Copy: $VAR1 = { 'Barney' => 'Betty', 'Fred' => 'Wilma' }; 0:10 >

    A backslash \ produces a reference to the same hash, but braces { } create a reference to a new, separate, anonymous hash containing a copy of the data in the original.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,