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

hi, New to perl. I am getting error saying cant coerce. What am I doing wrong ?
for( my $i = 0; $i <= $#columns; $i++){ push @{$first{$t_name}{$tab}{$sp}} , $ +columns[$i] ; } for my $fr ( sort keys %first ) { for my $tg ( sort keys %{ $first{$fr} +}) { for my $ab ( sort keys %{ $first{$fr} +{$tg} }) { foreach my $rt ( @{$first{$fr}{$tg}{$ab}}){ my $val = $first{$fr}{$tg}{$ab}{$rt} ; print " print $fr, $tg, $ab, $first{$fr}{$tg}{$ab}{$rt} "; } } } }
Thank you

Replies are listed 'Best First'.
Re: Cant coerce
by hdb (Monsignor) on Jun 24, 2014 at 18:52 UTC

    The line

    foreach my $rt ( @{$first{$fr}{$tg}{$ab}}){

    tells that $first{$fr}{$tg}{$ab} is a reference to an array.

    In the line

    my $val = $first{$fr}{$tg}{$ab}{$rt} ;

    you use it as if it were a reference to a hash. That's what the problem is.

    If you remove that line and change the one after to

    print " print $fr, $tg, $ab, $rt;"

    you will probably get what you want. (I cannot test as you have not provided runnable code nor data.)

Re: Cant coerce
by LanX (Saint) on Jun 24, 2014 at 18:34 UTC
    > What am I doing wrong ?

    you don't tell us the line number.

    > New to perl ...

    use diagnostics might help, and maybe a look into perldiag , there is only one possible error message that fits.

    Cheers Rolf

    (addicted to the Perl Programming Language)

Re: Cant coerce
by AppleFritter (Vicar) on Jun 24, 2014 at 18:22 UTC

    Howdy Anonymous Monk!

    Could you tell us what error message you're getting exactly? Also, the code you posted runs without errors. Could you please post code that shows the problem you're trying to get help with, as well as any data that might be necessary to reproduce it?

    Here's some nodes with helpful information for posting questions:

    You'll make it much easier for us to help you that way.

Re: Cant coerce
by Anonymous Monk on Jun 24, 2014 at 18:18 UTC

    Can't coerce what into what else? Please copy the error message precisely.

    It should also be noted that the code snippet you provided is not runnable to reproduce the problem. If you simplify your work down into a self-contained example that causes the problem, that process will typically lead you to understand what is going on. And if you don't, then the monks can quickly tell you what is going on because the sample code causes the issue and they can see it happening.