[Ma-linux] Perl 2D hash array

Przemek Klosowski przemek at jazz.ncnr.nist.gov
Tue Jun 30 09:28:14 EDT 2009


   > 	foreach $b (sort keys %$database{$a}){ 

   Try adding a pair of braces, making %{$database{$a}} :

   $ perl -e '$d={"a"=>{"b"=>"c"}}; print join(" ", keys(%{$d->{a}})), "\n";'

Thanks to Nicholas and Aaron: it's the braces instead of parens.  I
had a mental image of braces as syntax to distinguish hash array
access from simple array ($a{"3"} vs $a[3]), and other grouping being
done with parens, but clearly braces do more than that. For instance,
I grew up writing things like:

       perl -e '%a=("a","b"); print keys %a'

I haven't seen a good explanation of the syntax here: on one hand, %
is supposed to force the interpretation of a handle as a hash array
reference, on the other hand {} makes the content into a hash array:
for instance, %a={"a","b"} is a hash array whose first element is a
hash array pointer. Is the idea that % is used as LHS and {} as RHS?
What's your mental model of this syntax?



More information about the Ma-linux mailing list