Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Parsing hash from XML::Simple

by toolic (Bishop)
on May 16, 2016 at 19:01 UTC ( [id://1163157]=note: print w/replies, xml ) Need Help??


in reply to Parsing hash from XML::Simple

perldsc
use warnings; use strict; my $val = { 'precipitation' => { 'mode' => 'no' }, 'pressure' => { 'unit' => 'hPa', 'value' => '1031.65' }, 'temperature' => { 'min' => '81.15', 'value' => '81.15', 'max' => '81.15', 'unit' => 'fahrenheit' }, 'humidity' => { 'unit' => '%', 'value' => '89' }, 'visibility' => {}, 'wind' => { 'direction' => { 'value' => '202.503', 'name' => 'South-southwest', 'code' => 'SSW' }, 'gusts' => {}, 'speed' => { 'name' => 'Gentle Breeze', 'value' => '4.61' } }, 'clouds' => { 'name' => 'clear sky', 'value' => '8' }, 'weather' => { 'value' => 'clear sky', 'icon' => '02d', 'number' => '800' }, 'lastupdate' => { 'value' => '2016-05-16T18:27:33' }, 'city' => { 'name' => 'Pine Island Center', 'id' => '4168493', 'coord' => { 'lat' => '26.61', 'lon' => '-82.12' }, 'sun' => { 'rise' => '2016-05-16T10:40:09', 'set' => '2016-05-17T00:09:55' }, 'country' => 'US' } }; print $val->{city}{name}, "\n"; print $val->{city}{id}, "\n"; print $val->{city}{country}, "\n"; __END__ Pine Island Center 4168493 US

Replies are listed 'Best First'.
Re^2: Parsing hash from XML::Simple
by Anonymous Monk on May 16, 2016 at 19:09 UTC
    I am getting an error:
    Can't use string ("visibility") as a HASH ref while "strict refs" in u +se at
    Using a loop:
    foreach my $vals (keys %{$xml}) { print $vals->{city}{name}, "\n"; }
      Change "keys %{$xml}" to "values %$xml".

      UPDATE2: Added quotes around "values %$xml" .. apparently the OP missed the "values" keyword. My bad for assuming the meaning was clear.

      Alternatively, (if you want to keep using keys), try

      print $xml->{$vals}{city}{name}
      Update: The above assumes you have a HOH containing info on multiple cities.

      Your posted DUMP contains only ONE city. For that case, the "for loop" and "keys" does not make sense. simply use $xml->{city}{name}.

              This is not an optical illusion, it just looks like one.

        I know, but it will be multiple cities. And using this I am still geting the same error
        Can't use string ("visibility") as a HASH ref while "strict refs" in use at...
        with:
        foreach my $vals (%{$xml}) { print $vals->{city}{name}, "\n"; }
        When you say "does not make sense" do you mean it will also not run?/code foreach my $vals (%{$xml}) { print $vals-
        Thanks for the information, but unfortunately none of the answers helped me. I know I only posted one block of code for only one city, it will be more, but can this one block of code be used to show what I am trying to do? Can it still be looped and display the value of city->name? I tried you suggestion but if I run it it gives me this:
        Use of uninitialized value in print at...

        And here is the code as you posted
        ... foreach my $vals (values %{ $xml }) { print Dumper $vals->{city}{name}, "\n"; }
        Thanks for trying to help!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-04-23 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found