in reply to Correct syntax to access an array slice within a hash within a hash
i've used strict: don't code without it. it offers protection from many problems you might encounter during development.
#!/usr/bin/perl -w use strict; ## constants: i don't like 'use constant MAX => 3;', so... sub MAX() { 3 } sub SLICE() { 0 .. 1 } ## create empty hash reference ## ref to hash my $table_ref = {}; ## create some empty hashes ## ref to hash of hashes $table_ref->{$_} = () for( 0 .. MAX ); for( 0 .. MAX ) { if( $_ % 2 ) ## ref to hash of hashes holds scalar value { $table_ref->{$_}{shape} = 'rect' } else ## ref to hash of hashes holds scalar value { $table_ref->{$_}{shape} = 'circle' } ## ref to hash of hashes dereferenced as array, assign list @{ $table_ref->{$_}{coords} } = ( 0 .. MAX ); } for( sort keys %{ $table_ref } ) { print "Key: $_ Value: $table_ref->{$_}\n"; print "Shape $table_ref->{$_}{shape}\n"; ## ref to hash of hashes dereferenced as array, grab a slice print $_ for( @{ $table_ref->{$_}{coords} }[ SLICE ] ); print "\n"; }
~Particle *accelerates*
|
|---|