"can't coerce array into hash" problem.... 
Author Message
 "can't coerce array into hash" problem....

i have the following code:

%t=undef;



while..... { blah
    $t{$one[$i]}{$two[$i]}++;

Quote:
}

foreach my $key1 (sort keys %t) {

    foreach my $key2 (sort keys %{$t{$key1}}) {
        print FILE "$key1,$key2,$t{$key1}{$key2}\n";
    }

Quote:
}

which gives me a "can't coerce array into hash" error at the  second foreach
statement.

I've read a few things about hashes-where-array's expected, and vice versa,
but nothing obvious to tell me my problem.

Anyone any thoughts??



Fri, 14 May 2004 18:52:28 GMT  
 "can't coerce array into hash" problem....

Quote:

> %t=undef;

  ^^^^^^^^

"Odd number of elements in hash assignment ..."
That's not likely to be the cause of your problem, though.

Quote:



> while..... { blah
>     $t{$one[$i]}{$two[$i]}++;
> }

It might help if you actually investigated all those blahs and
dots; that's likely to be where the error is.  See if you can produce
a small code segment that exhibits the problem you describe.

Quote:
> foreach my $key1 (sort keys %t) {

>     foreach my $key2 (sort keys %{$t{$key1}}) {
>         print FILE "$key1,$key2,$t{$key1}{$key2}\n";
>     }
> }

What you've written here seems just fine; assuming $t{$key1} is
really a hashref in your initialization step:

  % perl -wle '$t{$_}{$_}++ for 1..3; foreach $k1 (sort keys %t){
    foreach $k2 (sort keys %{$t{$k1}}) { print "$k1,$k2,$t{$k1}{$k2}" }}'
  1,1,1
  2,2,1
  3,3,1

--
Joe Schaefer     "Always forgive your enemies; nothing annoys them so much."
                                               -- Oscar Wilde



Fri, 14 May 2004 22:51:14 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Can't coerce array into hash

2. Can't coerce array into hash

3. "Can't coerce array into hash"

4. "can't declare hash elem.."

5. hashes, -d's "x", and Data::Dumper

6. GD Problem (Won't "make")

7. What's the "halting problem"?

8. GD Problem (Won't "make")

9. HTTPGet "Can't connect" problem

10. problem passing 1st agrument with "eval 'exec perl -w -d -s $0 ${1+"$@"}'"

11. URGENT: Hash in Hash, Array in Array, Array in Hash, Hash in Array

12. "character class ""bug""

 

 
Powered by phpBB® Forum Software