gfairweather has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am trying to use a hash of hashes in a package but can't quite figure out how to print all the hashes with an accessor.
I create the hash something like this:
sub init_hash { my ($self) = @_; $self->{a_hash}{item1} = {name => 'a', value => '1'}; $self->{a_hash}{item2} = {name => 'b', value => '2'}; $self->{a_hash}{item3} = {name => 'c', value => '3'}; $self->{a_hash}{item4} = {name => 'd', value => '4'}; 1; }
sub print_item3 { my ($self) = @_; print $self->{a_hash}{item3}{name} . " : " . $self->{a_hash}{item3}{ +value} . "\n"; }
sub print_all_hash { my ($self) = @_; foreach my $item (keys $self->{a_hash}) { print $self->{a_hash}{$item}{name} . " : " . $self->{a_hash}{$item +}{value} . "\n"; } }
Type of arg 1 to keys must be hash (not hash element)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Package hash accessor
by CSJewell (Beadle) on Mar 31, 2009 at 22:48 UTC | |
by gfairweather (Novice) on Mar 31, 2009 at 22:52 UTC | |
|
Re: Package hash accessor
by targetsmart (Curate) on Apr 01, 2009 at 08:20 UTC | |
by Bloodnok (Vicar) on Apr 01, 2009 at 12:24 UTC |