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

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 }

Replies are listed 'Best First'.
Re^3: Can't use string ("2") as a HASH ref while "strict refs" in use
by chakram88 (Pilgrim) on Jul 17, 2008 at 18:05 UTC
    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
Re^3: Can't use string ("2") as a HASH ref while "strict refs" in use
by pileofrogs (Priest) on Jul 17, 2008 at 18:23 UTC

    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.