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

Blessed Perl monks, I've been on this problem for 6 hours now and I must submit. I need help. I'm trying to dereference a complex data structure and can't seem to figure it out. The data structure is referenced by a global variable: $IN Doing Data::Dumper($IN) yields:
$VAR1 = bless( { 'data_loaded' => 1, 'params' => {}, 'cookies' => { 'Community_Sesssion' => [ '68ce4195515 +b8040f9174503dacfe200' ], 'PHPSESSID' => [ 'e09347f6ca73f155b1fd +6500e92302b8' ], '__utmb' => [ '248415237' ], '__utmc' => [ '248415237' ], '__qcb' => [ '608960055' ], '__qca' => [ '1234223794-30138386-6175 +0767' ] }, 'p' => '', 'nph' => 0, '_debug' => 0 }, 'GT::CGI' );
What I'm trying to do is iterate through all the values of the cookies hash and do something with them. The last think I tried (out of about 100 other ways) is:
my %cookie_hash = $IN->{cookies}; foreach my $group (keys %cookie_hash) { print "The members of $group are\n"; foreach (@{$cookie_hash{$group}}) { print "\t$_\n"; } }
This outputs:
The members of HASH(0x828b958) are
Honestly, I'm stumped... Any help would be greatly appreciated.. Mike

Replies are listed 'Best First'.
Re: Dereferencing my hash?
by Fletch (Bishop) on Feb 10, 2009 at 20:03 UTC

    If you were running with warnings you would have gotten griped at for my %cookie_hash = $IN->{cookies};. To make a copy of a hashref into a hash you need to dereference it first: my $hash = %{ $hash_ref };

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Dereferencing my hash?
by Bloodnok (Vicar) on Feb 10, 2009 at 20:04 UTC
    I take it you haven't got strictures (use warnings; use strict;) enabled.

    Had you done so, you would probably have been able to tell that, in

    my %cookie_hash = $IN->{cookies};
    , you are attempting to assign a hash ref. to a hash (perlref)... try either
    my %cookie_hash = %{ $IN->{cookies} };
    or, the more usual,
    my $cookie_hash = $IN->{cookies};
    ...

    A user level that continues to overstate my experience :-))
Re: Dereferencing my hash?
by jethro (Monsignor) on Feb 10, 2009 at 20:07 UTC
    foreach my $group (keys %$cookie_hash) { # note the $
      Thanks to all... Your help got on the right track to here:
      my %hash = %{ $IN->{cookies} }; foreach my $k (keys %hash) { print "$hash{$k}[0]\n"; }
Re: Dereferencing my hash?
by AnomalousMonk (Archbishop) on Feb 11, 2009 at 01:37 UTC