Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Confused about accessing hashes

by ear (Acolyte)
on Apr 19, 2005 at 14:08 UTC ( [id://449251]=perlquestion: print w/replies, xml ) Need Help??

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

Was rather proud of myself. Wrote a script that takes a badly formated xml file and coverts it into something that XML::Simple can handle. But, now that XML::Simple successfully reads in the XML file and puts into a hash. I can't figure out how to access the hash. This part always confuses me. I'd appreciate not only help, but perhaps a good document written at kindergarten level that explains this stuff. After running, Dumper shows the hash was loaded like so:
$VAR1 = { 'ETPT' => { 'SERVER' => [ { 'RESPONSE' => '07:14:59,60,1236, +31789,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61 ,61,61,61,61,61,61,61,61,61,61,186,61,119,61,61,119,119,61,92,61,92,14 +754,1397,1475,1625,365,1085,1660,1249,1883,928,672,366002,1821 ,3591,3880,4505,4580,2877,1104,3165,4650,2420,1842,2548,2069,1382,3947 +,3913,4478,3096,2356,2857,3877,3398,3663,4134,1850,3661,5713,2 896,2173,4057,3161,1446,1497,3518,6443,4410,5154,11025,11907,8277,7890 +,5209,7109,5408,6133,5204,7057,6601,4907,7465,6403,6655,111717 ,5640,6052,6000. . . . 'CMD' => 'cq/quoteserver/qsclien +ts/client-X.X.X.13.1057|showgraphwbu@', 'CLIENT' => 'X.X.X.13.1057', 'IP' => 'X.X.X.41' }, { 'RESPONSE' => '07:14:59,60,618,3 +2,32,922,32,32,31,32,31,32,32,31,32,32,31,32,31,32,32,31,32,31 ,32,32,31,32,32,31,32,31,32,32,31,32,47,4047,32,31,32,32,1203,32,31,32 +,32,62,2063,31,32,469,31,32,32,31,32,31,32,32,31,32,31,32,32,3 1,32,32,31,63,1000,47,32,31,32,31,32,32,31,32,32,47,32,47,1906,32,31. +. . etc etc etc
I want to be able to pull out all of the RESPONSE's for each IP. (there are multiple RESPONSE's per IP). Help me please...

Replies are listed 'Best First'.
Re: Confused about accessing hashes
by ambs (Pilgrim) on Apr 19, 2005 at 14:14 UTC
    First, your $VAR1 is a reference to a hash with only one key, so
    $VAR1->{ETPT}
    is needed. This one is another reference to another hash with only the key 'SERVER', so
    $VAR1->{ETPT}{SERVER}
    Now, this is a reference to an array. You want to get all responses...
    $VAR1->{ETPT}{SERVER}[0]{RESPONSE} # First element of the array $VAR1->{ETPT}{SERVER}[1]{RESPONSE} # Second element of the array
    Probably you want to go for each element of the array like this:
    for (@{$VAR1->{ETPT}{SERVER}}) { # here $_->{RESPONSE} is each response of the array }
    Hope this helps.

    Alberto Simões

      Eureka! That was perfect and I actually get it now. Thanks MUCH.
      $data = $xml->XMLin ("good.xml"); . . . for (@{$data->{ETPT}{SERVER}}) { print "$_->{IP}\n\t$_->{RESPONSE}\n"; }
Re: Confused about accessing hashes
by RazorbladeBidet (Friar) on Apr 19, 2005 at 14:18 UTC
    Might want to try perlreftut because those are actually hash references :)

    But here's a quick synopsis that (hopefully) helps (note that this is not complete):
    Prepending: $ - a single item (scalar) @ - multiple items (array) % - hash Accessors: -> - dereference [] - array subscript {} - hash key
    If you work to combine those, then you should be able to access anything wrt hashes/arrays/keys.

    e.g. - $var1->{$var2}->[$var3]
    would return one element (the prepended $)
    from var1
    which is a reference (->)
    to a hash ( {}'s )
    with a key $var2
    whose value is a reference (->)
    to an array ([]'s)
    with an index $var3

    I hope that is more helpful than confusing :) The perldocs are much more complete.

    This is a rudimentary example, not intended to be 100% complete (fileglobs, lists, etc.) so please don't jump all over it (this addressed to the monastery at large :) )
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
Re: Confused about accessing hashes
by blazar (Canon) on Apr 19, 2005 at 14:19 UTC
    I want to be able to pull out all of the RESPONSE's for each IP. (there are multiple RESPONSE's per IP). Help me please...
    Then just use... ehm... another hash!! Precisely a "HoA" with the IPs as keys and arrayrefs contaning the various responses as values.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-19 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found