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

I am trying to fill in the hash on the fly. $HoH{"$G_Boardno"}={"$G_ExcepId"=>"{$G_CauseCode=>value}" }; This just adds a Key $G_Boardno with just $G_ExecId as value to it first time. In second iteration i am able to add the entire element as i have the key $G_Boardno already in place. How do i fix this problem.

Replies are listed 'Best First'.
Re: How to create a 4 dimension hash
by Andrew Coolman (Hermit) on Sep 22, 2008 at 17:23 UTC
    How about this? :
    $HoH{"$G_Boardno"}{"$G_ExcepId"}{$G_CauseCode} = "value";

    Regards,
    s++ą  ł˝ ął. Ş ş şą Żľ ľą˛ş ą ŻĽąş.}++y~-~?-{~/s**$_*ee
      Will this create the keys {$G_ExcepId}{$G_CauseCode} also ? As its an empty hash before this line:
      %HoH = () ; $HoH{"$G_Boardno"} = { "$G_ExcepId" => "{$G_CauseCode => value}" };

        Your code does not do what you think. Look at perldsc and Data::Dumper to dump out the data structure you're creating.

        "{$G_CauseCode => value}"

        is a string, not a hash reference. So your code

        $HoH{"$G_Boardno"} = { "$G_ExcepId" => "{$G_CauseCode => value}" };

        creates at the second level a hash mapping of the value of $G_ExcepId to the string {... => value}

        You could definitely say for sure on your own with Data::Dumper. Check it out.

        -Paul

        Will this create the keys {$G_ExcepId}{$G_CauseCode} also ?

        It's not the keys you should be worried about, but the values (the hash refs) at those keys. And yes, those are created. And thus, so are the keys.

        $HoH{"$G_Boardno"}{"$G_ExcepId"}{$G_CauseCode} = "value";

        is short for something like

        $HoH{"$G_Boardno"} //= {}; $HoH{"$G_Boardno"}{"$G_ExcepId"} //= {}; $HoH{"$G_Boardno"}{"$G_ExcepId"}{$G_CauseCode} = "value";

        due to autovivification. When an undefined value is derefenced, the appropriate value is created.

        >perl -wle"my($x,$y); $y=$x->{k}; print $x; HASH(0x225278) >perl -wle"my($x,$y); $y=$x->[0]; print $x; ARRAY(0x225278)
        Yes it will. You will get value by $G_Boardno which is hash from where you get value by $G_ExcepId which is also hash and so on. You can continue like this creating more and more levels until memory runs out.
        It is equal to :
        $HoH->{$G_Boardno}->{$G_ExcepId}->{$G_CauseCode} = "value";

        Regards,
        s++ą  ł˝ ął. Ş ş şą Żľ ľą˛ş ą ŻĽąş.}++y~-~?-{~/s**$_*ee
Re: How to create a 4 dimension hash
by GrandFather (Saint) on Sep 22, 2008 at 20:45 UTC

    You will find it helps in many ways (not least, you may solve the problem yourself) if you provide a small code sample that demonstrates the issue. Tell us what you expect to happen and what you think is happening. If you have a good case and a bad case, show data for both.

    By the way, you almost never need to interpolate variables into a string as the only content of the string. Also it would help you a lot to use a little white space so you can easily identify the "phrases" in your "sentence". Rewritten with those two points taken into consideration gives:

    $HoH{$G_Boardno} = {$G_ExcepId => "{$G_CauseCode=>value}"};

    where it is much easier to see that "{$G_CauseCode=>value}" is a string with a variable interpolated into it and not a nested hash reference as you probably expected.


    Perl reduces RSI - it saves typing
Re: How to create a 4 dimension hash
by dwm042 (Priest) on Sep 22, 2008 at 19:32 UTC
    Do it something like this:

    #!/usr/bin/perl use warnings; use strict; my %HoH; my $value = "I have a value!"; my $G_Boardno = "curiosity"; my $G_ExcepId = "killed_the"; my $G_CauseCode = "cat"; $HoH{"$G_Boardno"}{"$G_ExcepId"}{$G_CauseCode}="$value"; print "My hash = ", $HoH{curiosity}{killed_the}{cat}, "\n";
    With the result:

    C:\Code>perl hohohoh.pl My hash = I have a value!