On 2012-03-25 23:15, Chris Stinemetz wrote:
ok. You didn't do anything wrong per-se, all you did was try to go one
level too deep into your data structure.
$cell (eg 149) was the name of the key for the top-level %hash
container. All hash keys can only have one value associated with it. In
this case, the key 149 has a value of another hash (reference).
We then iterate over the keys for the hash that belongs to 149 key, and
set $hr to each key (eg: 01). This is where you get to
$href->{$cell}->{$hr}.
However, your second foreach attempts to extract a further set of hash
keys as the values of the $hr (01) level, but there aren't any. The
values associated to each $hr key are single non-reference scalar values
(2) or ("HD").
Because you tried to run 'sort keys' on these values, perl knows that it
can't treat a simple string as a hash key hence the error. It is
essentially (in this case) telling you that you're trying to perform
hash operations on something that isn't a hash.
foreach my $hr ( sort keys %{ $href->{$cell}} ) {
# print "\t$hr";
foreach my $count ( sort keys %{ $href->{$cell}->{$hr}} ) { #<-- line
48
print "\t$count";
}
}
My replacement code properly extracts the value from the last hash
layer, and prints it inline. (note that the -> is not needed beyond the
first instance in nested data structures when dereferencing). eg:
$href->{$cell}{$hr};
is valid, as is:
$aref->[0][1][2];
References seem to be the most confusing thing in any language. I assure
you though, that they are worth getting to a point where you are very
familiar with them. They contain endless possibilities :)
You did well. From what I could tell, your problem wasn't an issue with
the references themselves, it was you overlooked the layout of your data
structure. Your original Dumper output can help with that visually.
If there is anything I didn't explain well, just say so.
Good job on your attempts.
Steve
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/