Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with hash of hashes in perl

Tags:

hash

perl

I have a problem accessing variables in a hash of hashes I don't know what I have done wrong. While debugging the value of hash %list1 gives an undef, So I can't get my values out .

use strict ;
use warnings ;
my $text = "The, big, fat, little, bastards";
my $Author = "Alex , Shuman ,Directory";
my %hashes  = {1,2,3,40};
my %count  = ();
my @lst = split(",",$text);
my $i = 0 ;
my @Authors  =  split(",", $Author);
foreach  my $SingleAuthor(@Authors)
{
    foreach my $dig (@lst)
    {

     $count{$SingleAuthor}{$dig}++;
    }
}

counter(\%count);

sub counter
{
    my $ref  =  shift;
    my @SingleAuthors = keys %$ref;
    my %list1;
    foreach  my $SingleAuthor1(@SingleAuthors)
  {   
   %list1 =  $ref->{$SingleAuthor1};
    foreach my $dig1 (keys %list1)
    {

     print  $ref->{$SingleAuthor1}->{$dig1};
    }
}


}
like image 993
damola Avatar asked Jan 20 '26 19:01

damola


1 Answers

In two places you are attempting to assign a hash reference to a hash, which results in this warning: Reference found where even-sized list expected.

Here are two edits you need:

# I changed {} to ().
# However, you never use %hashes, so I'm not sure why it's here at all.
my %hashes  = (1,2,3,40);

# I added %{} around the right-hand side.
%list1 =  %{$ref->{$SingleAuthor1}};

See perlreftut for a useful and brief discussion of complex data structures.

For what it's worth, your counter() method could be simplified without loss of readability by dropping the intermediate variables.

sub counter {
    my $tallies = shift;
    foreach my $author (keys %$tallies) {   
        foreach my $dig (keys %{$tallies->{$author}}) {
            print $tallies->{$author}{$dig}, "\n";
        }
    }
}

Or, as ysth points out, like this if you don't need the keys:

    foreach my $author_digs (values %$tallies) {   
        print $dig, "\n" for values %$author_digs;
    }
like image 57
FMc Avatar answered Jan 23 '26 10:01

FMc