in reply to Can't use string ("2") as a HASH ref while "strict refs" in use

Basically, $Myhash{$item} == 2, not a ref to a hash like you think it does. Your problem is probably in where you assign the values, not in the code you showed us.

Also, why are you iterating over hashes to find a value? You could just use the hashes.

$item = $Myhash{$My_Value};

and so on...?

Try adding this:

use Data::Dumper; ... your code ... print Dumper(\%Myhash)."\n";

And see if %Myhash looks like you think it does.

Replies are listed 'Best First'.
Re^2: Can't use string ("2") as a HASH ref while "strict refs" in use
by Anonymous Monk on Jul 17, 2008 at 17:25 UTC
    Hi thanks for the response. please find the hash table that i defined. using the Data::Dumper the structure is desplayed as expected.
    %Myhash = ( 'SuperUser' => { 'Username' => '1', 'UserPage 0' => { 'UserPage_num' => '0', 'data' => { 'field1' => { 'name' => 'Version', 'Size' => '1' }, 'field2' => { 'name' => 'Length', 'Size' => '6' }, 'field3' => { 'name' => 'Number', 'Size' => '8' }, }, }, 'UserPage 1' => { 'UserPage_num' => '2', 'data' => { 'field1' => { 'name' => 'Version', 'Size' => '4' }, 'field2' => { 'name' => 'Length', 'Size' => '8' }, 'field3' => { 'name' => 'Number', 'Size' => '8' }, + }, }, And so on....

    Here all the values are predefined so i am not making any changes in the Hash table. I am just trying to retrieve the values from the hash table. Here is the code where i am getting issue with in line no 209
    206 foreach $item (keys %Myhash){ 207 if ($item eq $My_Value) { 208 foreach $iteminitem (sort(keys %{$Myhash{$item}})){ 209 foreach $iteminitem1 (sort(keys %{$Myhash{$item}{$iteminitem}} +)){ 210 if ($iteminitem1 eq 'name' && $user_number == $Myhash{$item}{ +$iteminitem}{$iteminitem1}) { 211 my $User = $iteminitem; 212 } 213 } 214 } 215 } 216 }
      as CountZero says -- you've got a situation where the you're referencing something that contains only a value and not a hash ref:

      Using your Data structure we walk trough your code and substitute in the values to see the problem.

      $My_Value = 'SuperUser'; # assumption for this exercise foreach $item (keys %Myhash){ # $item = 'SuperUser' first pass through if ($item eq $My_Value) { foreach $iteminitem (sort(keys %{$Myhash{'SuperUser'}})){ # $iteminitem = 'Username' first pass through foreach $iteminitem1 (sort(keys %{$Myhash{'SuperUser'}{'Us +ername'}})){ ## Here's the problem: $Myhash{'SuperUser'}{'Username'} = 2 if ($iteminitem1 eq 'name' && $user_number == $Myhash{$item}{ +$iteminitem}{$iteminitem1}) { my $User = $iteminitem; } } } } }

      What you probably want to do is check that you have a hash ref in $iteminitem

      wrap the line 209 loop in a check

      if (ref $iteminitem eq 'HASH') { foreach $iteminitem1 (sort(keys %{$Myhash{$item}{$iteminitem}})){ .... } }
      Or perhaps it's time to refactor the code a bit, but without further context that's difficult to say

      I have a hunch that one of the hashes is being interpreted as a list and then a scalar. That would produce what you're seeing. E.G. try this:

      print scalar(%hash = (name => "bob"));

      You get the number 2 because there's only two items in the list form of the hash (name and "bob").

      --Pileofrogs

      A reply falls below the community's threshold of quality. You may see it by logging in.