suzka99 has asked for the wisdom of the Perl Monks concerning the following question:

I have gone over all of the posts on this site, but I must
be doing something wrong because I get the following error:
"Can't use string("1") as a HASH ref while strict subs is on..."

I'm trying to create a matrix of authors who have
shared a thread, the 0 for never shared and 1+ for the
number of threads shared.

# get list of every author foreach $from (sort keys %authors) { foreach $subject(keys %subjects) { # populate buddy list @buddies = split(/\t/, $subjects{$subject}); # add each $buddy to %authors{$author} hash foreach $buddy(@buddies) { #this doesn't work # i want to add the buddy relationship if it does # not exist, else I want to increment the value ${$authors{$from}}{$buddy}++; } }

Thanks a lot in advance.

update (broquaint): s|<(/)?pre>|<${1}code>|g

Replies are listed 'Best First'.
Re: Adding values to a hash of hashes
by tachyon (Chancellor) on Mar 06, 2003 at 09:59 UTC

    The problem will be that somwhere in your code you have:

    $authors{$_}++ for @authors;

    This sets the value held in $authors{'some_author'} = 1 In a hash of hashes this value is not supposed to be 1 - it is required to be a reference to the nested hash. When you do ${$authors{$from}}{$buddy}++ #same as $authors{$from}->{$buddy}++ Perl is trying to dereference the value 1 which fails as you see. Try something like:

    # set authors hash to empty hash refs $authors{$_} = {} for @authors # you code goes here

    And it will work. BTW in your inner loop you might as well use values rather than keys as you only use the key to get the value anyway.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Yes, thank you. This was the problem.
      Once I changed the code from: $authors{$author}=1; to: $authors{$author} = {}; Everything started working.
      I was able to finally finish this in the afternoon, and I appreciate the advice.

      --Suzi (a she, not a he)

      I'd like to know if your guess was right.

        Given that the error message explicitly says that $authors{'douglas adams'} holds the value 1 (not 42 ;-) I will give you great odds.....of course he/she could be using $authors{'tolstoy'} = 1234567890 - 1234567889 or similar.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Adding values to a hash of hashes
by zby (Vicar) on Mar 06, 2003 at 09:55 UTC
    Are you sure that $authors{$from} near the end is a hash ref? I believe you should play with Data::Dumper or the debugger to inspect how the %authors hash looks like.
Re: Adding values to a hash of hashes
by Hofmator (Curate) on Mar 06, 2003 at 10:21 UTC
    It is advisable as well to use strict; together with tachyon's comment about the use of values in the innner loop this leads to:
    my (%authors, %subjects); # and populate these hashes somehow # the sort is irrelevant, at least for the part of the code you show for my $from (keys %authors) { $authors{$from}->{$buddy}++ for my $buddy map { split /\t/ } values %subjects; }
    and this seems rather strange because the values of %subjects never change, so each author ends up with exactly the same set of buddies. Is that what you intended? Or is some code missing/wrong in your post?

    -- Hofmator