Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

hash of hashes of hashes ...

by iza (Monk)
on Sep 18, 2001 at 15:38 UTC ( [id://113089]=perlquestion: print w/replies, xml ) Need Help??

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

sorry to post the question here but i haven't found it answered yet - i looked at the tutorials, faqs, q&a parts, made a search and still have no info so here it is

my hash is build in parsing an xml document (my $hash_name = XMLin($r +esult);) if i print Dumper($hash_name{'element1'}->{'element2'}), i get : $VAR1 = { '365' => { another hash of hashes ... }, '302' => { another one } };

the point is that i want to retrieve '365' and/or '302' - they are not preset values

i thought that doing

my %tmp = $hash_name{'element1'}->{'element2'};

and then using keys %tmp would give me those values, but it doesn't so please could someone help me ???

edit: Petruchio Tue Sep 18 11:55:13 UTC 2001 - Added markup

Replies are listed 'Best First'.
Re: hash of hashes of hashes ...
by davorg (Chancellor) on Sep 18, 2001 at 15:48 UTC

    (please learn how to format code readably with <code> tags)

    You should read perldoc perlreftut and perldoc perldsc for more explaination on this, but the basic solution to your problem is:

    my %tmp = %{$hash_name{'element1'}->{'element2'}};
    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

Re: hash of hashes of hashes ...
by Zaxo (Archbishop) on Sep 18, 2001 at 15:59 UTC

    I'm a little uncertain of your formatting, I'll check back to see if I've misunderstood the question. Assuming $hash_name is a hashref, $hash_name->{'element1'}{'element2'} is a hashref to the data you want. To assign to a hash, dereference:

    my %tmp = %{$hash_name->{'element1'}{'element2'}};
    You may not need the %tmp hash, that is a matter of convenience and depends on how much you need to manipulate the data.

    Update: Okay, I checked back. It's still unclear whether you have hash %hash_name or a hashref $hash_name. davorg's reply assumes the hash, supported by your usage of the arrow op. I assume a hashref because because of the form of its initial assignment. Checking, that's what XML::Simple's XMLin() returns. Are you using that module?

    After Compline,
    Zaxo

      yes, i was using XML::Simple module
      and yes davorg's solution is working :)
      thank you SO MUCH :))
      and yes, i'll learn how to format my code, and re-read again and again the doc on references (i NEED to read it again and again ....)
Re: hash of hashes of hashes ...
by graq (Curate) on Sep 18, 2001 at 16:35 UTC
    I will ignore the 'print Dumper' line and concentrate on your $VAR1 (which is a hash reference).

    My understanding of your question is:
    '365' and '302' are examples of keys in a hash whose hash values are a reference to a hash of hashes.

    To retrive the values '365' and '302' you need just use:

    my @hash_refs = keys %{$VAR1};
    Then again, I could be misinterpreting your question!
    If you are still in doubt, post a useful code snippet.

    EXAMPLE

    #!/usr/bin/perl -w use strict; my $VAR1 = { '365' => { foo=>1, bar=>2 }, '302' => {} }; my @hash_refs = keys %{$VAR1}; print "My hash refs are: ".join (', ',@hash_refs)."\n"; print "The value of my hash refs are: ".join(', ', values %{$VAR1})."\ +n\n"; my %tmp = %$VAR1; print "'bar' in '365' is: ".$tmp{'365'}->{bar}."\n";

    --
    Graq

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://113089]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2025-02-09 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (95 votes). Check out past polls.