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

I am trying to run through a hash an check certain values from each key to re-assign the keys to other hashes. I have tried many different logic structures and I keep arriving at the same error "Can't coerce array into hash" I would appreciate some aoutside perspective, here is my current sub-routine:
sub check_dcr { my(@keys) = keys %temp_dcr; foreach $key (@keys) { if ("$temp_dcr{$key}{RightNavLaunchDate}" le $today) { if ("$temp_dcr{$key}{ShowPersonal}" eq "Personal") { $pers_dcr_files{$key} = $temp_dcr{$key}; } if ("$temp_dcr{$key}{ShowHome}" eq "Home") { $home_dcr_files{$key} = $temp_dcr{$key}; } if ("$temp_dcr{$key}{ShowBusiness}" eq "Business") { $bus_dcr_files{$key} = $temp_dcr{$key}; } } # End if statement to check for Launch Date } # End foreach loop to check all keys in the temporary hash } # End routine to check the temp hash and send out keys to specified +hashes
The program does not even seem to get into the first if statement... thanks, bill

Replies are listed 'Best First'.
Re: Hash Problems
by chipmunk (Parson) on May 16, 2001 at 23:39 UTC
    You're using the values of the array as hash references $temp_dcr{$key}{RightNavLaunchDate}; are you sure that they are actually hash references and not array references?

    perldiag:

    Can't coerce array into hash (F) You used an array where a hash was expected, but the array has no information on how to map from keys to array indices. You can do that only with arrays that have a hash reference at index 0.
      How can I take the array out of the equation? I have tried using foreach, while loops etc... and I can't seem to get around the array. I just want to go through each key of temp_dcr and compare some of the values. Would there be a way to do this using the each function?
        The array of keys is not a problem. That's not what I was referring to.

        My suspicion is that the value of $temp_dcr{$key} is not a reference to a hash, which is what you are using it as in this snippet of code. I think that it is actually a reference to an array.

        An easy way to check is with Data::Dumper:

        use Data::Dumper; print Dumper %tmp_dcr; # if %tmp_dcr is too big, just dump $tmp_dcr{$key} for one of the keys
Re: Hash Problems
by Anonymous Monk on May 16, 2001 at 23:37 UTC
    Read your Answer in the Questions Section, where you have posted before.
Re: Hash Problems
by Anonymous Monk on May 16, 2001 at 23:46 UTC
    Show us the Data::Dumper output of %temp_dcr. It helps.
      Here is the output:
      $VAR1 = 'Test1'; $VAR2 = { 'ShowHome' => 'Personal', 'RightNavExpirationDate' => 20010521, 'RightNavLaunchDate' => 20010515, 'SmallGraphic' => '/templatedata/components/msn.gif' }; $VAR3 = 'Name'; $VAR4 = [ 'RightNavLaunchDate', '', 'RightNavExpirationDate', '', 'LinkURL', '', 'SmallGraphic', '', 'ShowHome', '', 'ShowBusiness', '', 'ShowPersonal', '' ]; $VAR5 = 'Test2'; $VAR6 = { 'ShowHome' => 'Home', 'RightNavExpirationDate' => 20010520, 'RightNavLaunchDate' => 20010510, 'ShowPersonal' => 'Personal', 'SmallGraphic' => '/templatedata/components/msn.gif' }; $VAR7 = 'PromoPH'; $VAR8 = { 'ShowHome' => 'Home', 'RightNavExpirationDate' => 20001215, 'ShowPersonal' => 'Personal', 'SmallGraphic' => '2000-12-13' }; $VAR9 = 'TestLinkPH'; $VAR10 = { 'LinkURL' => '/templatedata/components/test.asp', 'RightNavLaunchDate' => 'Home', 'ShowPersonal' => 'Personal', 'SmallGraphic' => '/templatedata/components/msn.gif' }; $VAR11 = 'OfferNavP'; $VAR12 = { 'ShowHome' => 'Personal', 'RightNavExpirationDate' => 20000517, 'RightNavLaunchDate' => 20000516, 'SmallGraphic' => '/templatedata/components/msn.gif' }; $VAR13 = 'PromoP'; $VAR14 = { 'ShowHome' => 'Personal', 'RightNavExpirationDate' => 29991620, 'RightNavLaunchDate' => 20000618, 'SmallGraphic' => '/templatedata/components/msn.gif' }; $VAR15 = 'TestLinkP'; $VAR16 = { 'LinkURL' => '/templatedata/components/test.asp', 'ShowHome' => 'Personal', 'RightNavExpirationDate' => 20000430, 'RightNavLaunchDate' => 20000421, 'SmallGraphic' => '/templatedata/components/msn.gif' }; $VAR17 = 'OfferNavPH'; $VAR18 = { 'ShowHome' => 'Home', 'RightNavExpirationDate' => 20000523, 'RightNavLaunchDate' => 20000519, 'ShowPersonal' => 'Personal', 'SmallGraphic' => '/templatedata/components/msn.gif' }; Can't coerce array into hash at C:\Program Files\Interwoven\TeamSite\l +ocal\bin\G enerate_Right_Nav.ipl line 199.
        Where do you insert the 'Name' key? It is the Array. It is an other Value than the other keys.