damola
damola

Reputation: 292

Problems with hash of hashes in 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};
    }
}


}

Upvotes: 2

Views: 4859

Answers (2)

FMc
FMc

Reputation: 42421

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;
    }

Upvotes: 5

Dave Cross
Dave Cross

Reputation: 69314

When I ran your code, Perl told me exactly what the problem was.

$ ./hash
Reference found where even-sized list expected at ./hash line 7.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.
Reference found where even-sized list expected at ./hash line 30.
Use of uninitialized value in print at ./hash line 34.

If that's unclear, you can turn on diagnostics to get more detailed descriptions.

$ perl -Mdiagnostics hash
Reference found where even-sized list expected at hash line 7 (#1)
    (W misc) You gave a single reference where Perl was expecting a list
    with an even number of elements (for assignment to a hash). This usually
    means that you used the anon hash constructor when you meant to use
    parens. In any case, a hash requires key/value pairs.

        %hash = { one => 1, two => 2, };    # WRONG
        %hash = [ qw/ an anon array / ];    # WRONG
        %hash = ( one => 1, two => 2, );    # right
        %hash = qw( one 1 two 2 );          # also fine

Upvotes: 4

Related Questions