Problems with hash of hashes in perl -
i have problem accessing variables in hash of hashes don't know have done wrong. while debugging value of hash %list1 gives undef, can't values out .
use strict ; use warnings ; $text = "the, big, fat, little, bastards"; $author = "alex , shuman ,directory"; %hashes = {1,2,3,40}; %count = (); @lst = split(",",$text); $i = 0 ; @authors = split(",", $author); foreach $singleauthor(@authors) { foreach $dig (@lst) { $count{$singleauthor}{$dig}++; } } counter(\%count); sub counter { $ref = shift; @singleauthors = keys %$ref; %list1; foreach $singleauthor1(@singleauthors) { %list1 = $ref->{$singleauthor1}; foreach $dig1 (keys %list1) { print $ref->{$singleauthor1}->{$dig1}; } } }
in 2 places attempting assign hash reference hash, results in warning: reference found even-sized list expected.
here 2 edits need:
# changed {} (). # however, never use %hashes, i'm not sure why it's here @ all. %hashes = (1,2,3,40); # added %{} around right-hand side. %list1 = %{$ref->{$singleauthor1}};
see perlreftut useful , brief discussion of complex data structures.
for it's worth, counter()
method simplified without loss of readability dropping intermediate variables.
sub counter { $tallies = shift; foreach $author (keys %$tallies) { foreach $dig (keys %{$tallies->{$author}}) { print $tallies->{$author}{$dig}, "\n"; } } }
or, ysth points out, if don't need keys:
foreach $author_digs (values %$tallies) { print $dig, "\n" values %$author_digs; }
Comments
Post a Comment