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

Hi Monks!

I am trying to parse this data, wich is the result of this code my $xml = XMLin($rss); using XML::SIMPLE
I am lost here, trying to use this, but how to get to this value city->name. If I can get the name in city I can figure it out how to get to the others I also need. With the foreach I can only get to the keys:
foreach my $vals ( keys %{ $xml }){ print Dumper $vals; }
Test Data:
$VAR1 = { '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' } };
Thank you for helping! using XML::SIMPLE

Replies are listed 'Best First'.
Re: Parsing hash from XML::Simple
by toolic (Bishop) on May 16, 2016 at 19:01 UTC
    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
      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.

Re: Parsing hash from XML::Simple
by jellisii2 (Hermit) on May 17, 2016 at 13:21 UTC