Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

printing refence of hash inside a hash

by opensourcer (Monk)
on May 12, 2007 at 06:29 UTC ( #615049=perlquestion: print w/replies, xml ) Need Help??
opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

I'm unable to print values of hash who is inside of another hash. I'm trying to print values of version and type.
$data = { data => 'hello', complicated => { version => 1, type => 'struct', }, req => 'Submit' }; foreach (keys %$data ) { if (ref($data->{$_})) { foreach (keys %{$data->{$_}}) { print " $_ : ".$data->{$_}->{$_}."\n"; } } }
can any 1 help me with this .

Replies are listed 'Best First'.
Re: printing refence of hash inside a hash
by Samy_rio (Vicar) on May 12, 2007 at 06:39 UTC

    Hi, try like this,

    $data = { data => 'hello', complicated => { version => 1, + type => 'struct', }, req => 'Submit' }; foreach my $first (keys %$data ) { if (ref($data->{$first})) { foreach my $second (keys %{$data->{$first}}) { print " $second : ".$data->{$first}->{$second}."\n"; } } }

    Velusamy R.

    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: printing refence of hash inside a hash
by liverpole (Monsignor) on May 12, 2007 at 14:13 UTC
    Hi opensourcer,

    What shmem points out is that you're apparently trying to use $_ to refer to 2 separate (distinct) variables, which you can't do.

    Samy_rio demonstrated one solution, which is to name the loop variable in each of the loops, so that there isn't a naming conflict.

    I wanted to mention an alternative way of viewing the contents of a (possibly complicated) data structure, which is quite commonly used -- Data::Dumper.

    For example:

    use strict; use warnings; use Data::Dumper; my $data = { data => 'hello', complicated => { version => 1, type => 'struct', }, req => 'Submit' }; print "Contents of \$data: ", Dumper($data), "\n";

    which prints:

    Contents of $data: $VAR1 = { 'req' => 'Submit', 'complicated' => { 'version' => 1, 'type' => 'struct' }, 'data' => 'hello' };

Re: printing refence of hash inside a hash
by shmem (Canon) on May 12, 2007 at 09:34 UTC
    print " $_ : ".$data->{$_}->{$_}."\n";

    So perl should figure out for you which $_ to use - the one from the inner loop first, then the outer, then the inner?

    for (qw(fine)) { for (qw(mess)) { print "$_ $_\n"; } } __END__ mess mess

    No "fine mess".


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: printing refence of hash inside a hash
by RL (Monk) on May 12, 2007 at 16:08 UTC

    The following is a bit carried away but I think it might be usefull one way or the other.

    As far as I know

    foreach (keys %$data ) { bla....

    causes $data to be deferenced on each iteration of the loop, which may become expensive if you have a huge amount of stuff in $data

    One may extract the keys before the loop, use a temporary array which will be used for the loop (and while freeing resources) like this:

    my @tmp_ary = keys %$data; while(my $item = shift @tmp_ary) { do stuff... }
    After the loop @tmp_ary will still be there but empty.

    Hope this helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://615049]
Approved by Corion
[Discipulus]: ahi ahi ahi!!
[Lady_Aleena]: paco is a little over 2 years before my time.
[Discipulus]: you can try tachyon's iterative approach Re: Win32 Recursive Directory Listing
[Lady_Aleena]: I'm on Linux/
[beech]: isn't 2 year about standard recovery time for that kind of surgery?
[Discipulus]: if you hate FIle::Find
[Lady_Aleena]: I don't hate it, it just drives me crazy.
[Discipulus]: LA the solution is portable to whatever

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-04-23 20:42 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.