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

Is it possible to assign the value to key as another Hash? If yes, how? and how do I access the key/value pair of Hash2? My code is something like this...where am I going wrong? I guess somewhere while assigning!!!! Thanks for the help in advance...
my %class_room = ("Title1","Location1"); my %date_class_room; my $s_date = "date1"; $date_class_room{$s_date} = %class_room; for (keys %class_room) { print "Class_Room KEY: $_ VALUE $class_room{$_}\n"; } for (keys %date_class_room) { $a = $_; @x = $date_class_room{$_}; print @x, "\n"; %b = @x; print $a, "\n"; for (keys %b) { $c = $_; $d = $b{$_}; print $c, $d, "\n"; } }

Replies are listed 'Best First'.
Re: Assigning Hash to Hash
by Errto (Vicar) on Nov 29, 2005 at 05:13 UTC

    What you need are references. The crucial line is this one

    $date_class_room{$s_date} = %class_room;
    which should instead be
    $date_class_room{$s_date} = \%class_room;
    The rest of your code you can keep pretty much the same, although I have to say storing the contents of a hash (%class_room) into an array (@x) and then into another hash (%b) is kind of a weird thing to do, but it will work. The only thing you need to change is the line where you assign to @x:
    @x = %{$date_class_room{$_}};
    What this means is that because up above we stored a reference to a hash as the value in our main hash, we can use the %{} construct to turn that reference back into a real hash - in this case, to produce a list of keys and values. The link above points to a tutorial on this and you can also look in Tutorials.

    As a personal quip, if it were up to me, $scalar = %hash would either do the same thing as $scalar = \%hash or be a compile-time error.

      Thanks a lot! I had tried using reference but then did not assign the value to @x properly. Now, I know where I went wrong. As for ur other comment on "storing the contents of a hash (%class_room) into an array (@x) and then into another hash (%b) is kind of a weird thing to do,"....Well, that was just one of my trial & error code which I just accidently pasted. Thanks again.
Re: Assigning Hash to Hash
by Discipulus (Canon) on Nov 29, 2005 at 13:47 UTC
    there my little example

    the way is to assign as value the $ref built as $ref = \%hash;

    my %h1=('qwqw' => {'qw' => 'qww','df' => 'dfff','C1' => {'A2' => a2,'B +2' => 'b2','C2' => 'c2END'}},); my %h2=( 'AAA' => 'aaa', 'BBB' => 'bbb', 'CCC' => {'A1' => a1,'B1' => 'b1','C1' => {'A2' => a2,'B2' => 'b2','C2 +' => 'c2END'}}, 'DDD' => 'ddd', 'EEE' => \%h1, ); my $ref2 = \%h2; &ddump($ref2); sub ddump { my $ref = shift; my $deep = shift||0; foreach my $k (sort keys %{$ref}) { if (ref( ${$ref}{$k})) {print "\t" x $deep."$k =>\n"; &dd +ump (${$ref}{$k}, ($deep+1))} else {print "\t" x ($deep)."$k => ${$ref}{$k}\n";} $deep-1; } }


    If u are intersted in hash jungling my better surpise was:
    # %h2 as above my @arr = qw(AAA CCC EEE); my %newH; @newH{@arr}=@h2{@arr};
    ciao Lor*