in reply to Re: How to declare a hash of array?
in thread How to declare a hash of array?
An example:
Result:my %hash; $hash{beta} = 'x'; for my $name ( qw(alpha beta) ) { push @{$hash{$name}}, 123, 456; } use Data::Dumper; print Dumper \%hash;
So... where's the array data for 'beta'? It's in the global variable @x. Witness: append this line to the above script:$VAR1 = { 'alpha' => [ 123, 456 ], 'beta' => 'x' };
Result:print Dumper \@x;
$VAR1 = [ 123, 456 ];
With strict enabled, it'll get caught while trying to push the data: insert the line
at the top of the script and comment out the line, dumping @x, we appended earlier:use strict;
Can't use string ("x") as an ARRAY ref while "strict refs" in use at t +est.pl line 6.
Nasty if you didn't expect it.
The risk of running into it is greatest when using data structures of unequal depth.
|
|---|