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

I'm at my wit's end trying to figure out why this isn't working, and have visited many of the wonderfully instructive tutorials on references here intro to references and the great responses to similar questions from others, but i still can't seem to solve this! I realize that i must be confusing hashes for arrays somewhere, but it's not clear to me where?

i am reading records from a database (MS-Access, unfortunately) on a Win2000 server, where the records have responses from judges ($j_name) who have scored three elements($mentor, $ptcare,$clinres) of a nomination($n_name). I want to generate a report of all the scores submitted by each judge for multiple nominees, so an array in a hash of arrays seemed like the logical way to go. It works fine if i comment out line 52 (i get the expected hash of arrays, showing which nominees were judged by which judges), but when i add back line 52, I get the above error "Can't coerce array into hash....at line 52". i've also tried using the syntax: "$judge{$j_name}->{Nominee}->{Scores} = [$mentor,$ptcare,$clinres]; " with the same error. i'm sure i'm making some kind of dereferencing error, i just can't see it. TIA for your kind and patient help!

40 my %judge = (); 41 while ($db->FetchRow()) { 42 my $n_name = $db->Data("NOMNAME"); 43 my $mentor = $db->Data("MENTOR"); 44 my $ptcare = $db->Data("PTCARE"); 45 my $clinres = $db->Data("CLINRES"); 46 my $j_name = $db->Data("JUDGENAME"); 47 my $j_inst = $db->Data("JUDGEINST"); 48 print "<p>$j_name, $j_inst, $n_name</p>"; 49 $judge{$j_name} = {JudgeInst => $j_inst} 50 unless exists $judge{$j_name}; 51 push @{$judge{$j_name}->{Nominee}}, $n_name; 52 push @{$judge{$j_name}->{Nominee}->{Scores}}, + $mentor,$ptcare,$clinres; }

Replies are listed 'Best First'.
Re: Can't coerce array into hash
by seaver (Pilgrim) on Sep 24, 2003 at 15:17 UTC
    OK, I think I can tell you what your problem is, it's an easy mistake to make as hashes can be so confusing sometimes.

    Basically, after line 51 you have an single entry array [$n-name] in:

    $judge{$j_name}->{Nominee}
    Which is fine, but this means, if you call:
    $judge{$j_name}->{Nominee}
    you get [$n-name]. And you MAKE that call, in line 52, when you say:
    $judge{$j_name}->{Nominee}->{Scores}
    So you're turning around and saying that [$n-name] is also %Scores. Hence the error.

    Solution:

    51 push @{$judge{$j_name}->{Nominee}->{$n_name}->{Scores}}, $mentor,$ptcare,$clinres;

    OR

    51 push @{$judge{$j_name}->{Nominee}->{$n_name}}, $mentor,$ptcare,$clinres;

    Hope I got that right.

    Cheers
    Sam

      Thanks so much for the quick replies!!! both solutions worked, although i don't really need the extra "Scores" key that is generated by the first solution.

      Another solution is:

      push @{$judge{$j_name}->{Nominee}},$n_name => [$mentor,$ptcare,$clinre +s];

      Thanks again!

Re: Can't coerce array into hash
by rdfield (Priest) on Sep 24, 2003 at 15:10 UTC
    Line 51 references the "Nominee" element as an array reference, then you go an use "Nominee" as a hash reference by adding "->{Score}".

    rdfield