perl Data::Dumper to extract key values

好久不见. 提交于 2021-02-10 22:10:56

问题


I have some perl code which I've written to get weather data/alerts from NOAA.

My code is pretty simple:

use Weather::NOAA::Alert;
use Data::Dumper;

$alert = Weather::NOAA::Alert->new(['TXC301']);

$events = $alert->get_events();
$alert->poll_events();

print Dumper($events);

# @url = (keys %{$VAR1->{'TXC301'}});
# $url = $VAR1->{'TXC301'};
$url = $events->{'TXC301'};

print "URL is $url\n";

# $expires= $events->{'TXC301'}->{$url}->{'expires'};

$expires= $events->{'TXC301'}->{'http://alerts.weather.gov/cap/wwacapget.php?x=TX12516CBE9400.FloodWarning.12516CC068C0TX.MAFFLWMAF.f21e7ce7cf8e930ab73a110c4d912576'}->{'expires'};

print "Expires:  $expires\n";

The output:

$VAR1 = {
          'TXC301' => {
                        'http://alerts.weather.gov/cap/wwacapget.php?x=TX12516CBE9400.FloodWarning.12516CC068C0TX.MAFFLWMAF.f21e7ce7cf8e930ab73a110c4d912576' => {
                                                                                                                                                                 'certainty' => 'Likely',
                                                                                                                                                                 'senderName' => 'NWS Midland-Odessa (Western Texas and Southeastern New Mexico)',
                                                                                                                                                                 'urgency' => 'Expected',
                                                                                                                                                                 'instruction' => 'A FLOOD WARNING MEANS THAT FLOODING IS IMMINENT OR HAS BEEN REPORTED.
STREAM RISES WILL BE SLOW AND FLASH FLOODING IS NOT EXPECTED.
HOWEVER... ALL INTERESTED PARTIES SHOULD TAKE NECESSARY PRECAUTIONS
IMMEDIATELY.
DO NOT DRIVE YOUR VEHICLE INTO AREAS WHERE THE WATER COVERS THE
ROADWAY. THE WATER DEPTH MAY BE TOO GREAT TO ALLOW YOUR CAR TO CROSS
SAFELY.',
                                                                                                                                                                 'description' => 'THE NATIONAL WEATHER SERVICE IN MIDLAND HAS ISSUED A
   FLOOD WARNING FOR...
SOUTHWESTERN LOVING COUNTY IN WEST TEXAS...
NORTHWESTERN WARD COUNTY IN WEST TEXAS...
NORTH CENTRAL REEVES COUNTY IN SOUTHWEST TEXAS...
   UNTIL 300 PM CDT FRIDAY
   AT 259 AM CDT...ROADS REMAIN CLOSED NEAR THE PECOS RIVER BETWEEN
RED BLUFF AND INTERSTATE 20 BECAUSE OF ELEVATED RIVER LEVELS DUE
TO RECENT RAINS. FLOODING WILL ALSO IMPACT THE CITY OF PECOS.',
                                                                                                                                                                 'event' => 'Flood Warning',
                                                                                                                                                                 'delete' => 0,
                                                                                                                                                                 'category' => 'Met',
                                                                                                                                                                 'severity' => 'Moderate',
                                                                                                                                                                 'effective' => '2014-09-26T03:00:00-05:00',
                                                                                                                                                                 'headline' => 'Flood Warning issued September 26 at 3:00AM CDT until September 26 at 3:00PM CDT by NWS Midland-Odessa',
                                                                                                                                                                 'expires' => '2014-09-26T15:00:00-05:00'
                                                                                                                                                               }
                      }
        };
URL is HASH(0x26384c0)
Expires:  2014-09-26T15:00:00-05:00

The TXC301 is a report identifier.

The output of the script will print all the values fetched from NOAA.

The goal is to store/return the 'expires' value.

I have lines commented out, which were attempts at achieving my goal.

The problem I'm having is getting the $url variable. I need this value in order to get my $expires value. The 2nd to last line in my code will correctly get the $expires value, but in order to do this I needed to hard code the URL into the line.

I'm trying to get the line directly above that (3rd to last) to work:

 $expires= $events->{'TXC301'}->{$url}->{'expires'};

But this depends on the $url value to be stored. I can't seem to figure out how to get the $url value.

My guesses:

@url = (keys %{$VAR1->{'TXC301'}});
$url = $VAR1->{'TXC301'};
$url = $events->{'TXC301'};

None of which work.

Any help would be great.

Thanks!

Regards,

Joseph Spenner


回答1:


Given there is only one value for that level of hash ref, you could use values:

print +( values %{ $VAR1->{TXC301} } )[0]{expires}, "\n";

Outputs:

2014-09-26T15:00:00-05:00

Alternative to Data::Dumper

Also, on a separate issue, I would like to recommend the use of Data::Dump over the core library Data::Dumper.

The default settings and features for this alternative give superior output and enable one to analyze a data structure a lot quicker as demonstrated below:

$VAR1 = {
          'TXC301' => {
                        'http://alerts.weather.gov/cap/wwacapget.php?x=TX12516CBE9400.FloodWarning.12516CC068C0TX.MAFFLWMAF.f21e7ce7cf8e930ab73a110c4d912576' => {
                                                                                                                                                                 'certainty' => 'Likely',
                                                                                                                                                                 'senderName' => 'NWS Midland-Odessa (Western Texas and Southeastern New Mexico)',
                                                                                                                                                                 'urgency' => 'Expected',
                                                                                                                                                                 'instruction' => 'A FLOOD WARNING MEANS THAT FLOODING IS IMMINENT OR HAS BEEN REPORTED.
STREAM RISES WILL BE SLOW AND FLASH FLOODING IS NOT EXPECTED.
HOWEVER... ALL INTERESTED PARTIES SHOULD TAKE NECESSARY PRECAUTIONS
IMMEDIATELY.
DO NOT DRIVE YOUR VEHICLE INTO AREAS WHERE THE WATER COVERS THE
ROADWAY. THE WATER DEPTH MAY BE TOO GREAT TO ALLOW YOUR CAR TO CROSS
SAFELY.',
                                                                                                                                                                 'description' => 'THE NATIONAL WEATHER SERVICE IN MIDLAND HAS ISSUED A
   FLOOD WARNING FOR...
SOUTHWESTERN LOVING COUNTY IN WEST TEXAS...
NORTHWESTERN WARD COUNTY IN WEST TEXAS...
NORTH CENTRAL REEVES COUNTY IN SOUTHWEST TEXAS...
   UNTIL 300 PM CDT FRIDAY
   AT 259 AM CDT...ROADS REMAIN CLOSED NEAR THE PECOS RIVER BETWEEN
RED BLUFF AND INTERSTATE 20 BECAUSE OF ELEVATED RIVER LEVELS DUE
TO RECENT RAINS. FLOODING WILL ALSO IMPACT THE CITY OF PECOS.',
                                                                                                                                                                 'event' => 'Flood Warning',
                                                                                                                                                                 'delete' => 0,
                                                                                                                                                                 'category' => 'Met',
                                                                                                                                                                 'severity' => 'Moderate',
                                                                                                                                                                 'effective' => '2014-09-26T03:00:00-05:00',
                                                                                                                                                                 'headline' => 'Flood Warning issued September 26 at 3:00AM CDT until September 26 at 3:00PM CDT by NWS Midland-Odessa',
                                                                                                                                                                 'expires' => '2014-09-26T15:00:00-05:00'
                                                                                                                                                               }
                      }
        };

use Data::Dump;
dd $VAR1;

Outputs:

{
  TXC301 => {
    "http://alerts.weather.gov/cap/wwacapget.php?x=TX12516CBE9400.FloodWarning.12516CC068C0TX.MAFFLWMAF.f21e7ce7cf8e930ab73a110c4d912576" => {
      category    => "Met",
      certainty   => "Likely",
      delete      => 0,
      description => "THE NATIONAL WEATHER SERVICE IN MIDLAND HAS ISSUED A\n   FLOOD WARNING FOR...\nSOUTHWESTERN LOVING COUNTY IN WEST TEXAS...\nNORTHWESTERN WARD COUNTY IN WEST TEXAS...\nNORTH CENTRAL REEVES COUNTY IN SOUTHWEST TEXAS...\n   UNTIL 300 PM CDT FRIDAY\n   AT 259 AM CDT...ROADS REMAIN CLOSED NEAR THE PECOS RIVER BETWEEN\nRED BLUFF AND INTERSTATE 20 BECAUSE OF ELEVATED RIVER LEVELS DUE\nTO RECENT RAINS. FLOODING WILL ALSO IMPACT THE CITY OF PECOS.",
      effective   => "2014-09-26T03:00:00-05:00",
      event       => "Flood Warning",
      expires     => "2014-09-26T15:00:00-05:00",
      headline    => "Flood Warning issued September 26 at 3:00AM CDT until September 26 at 3:00PM CDT by NWS Midland-Odessa",
      instruction => "A FLOOD WARNING MEANS THAT FLOODING IS IMMINENT OR HAS BEEN REPORTED.\nSTREAM RISES WILL BE SLOW AND FLASH FLOODING IS NOT EXPECTED.\nHOWEVER... ALL INTERESTED PARTIES SHOULD TAKE NECESSARY PRECAUTIONS\nIMMEDIATELY.\nDO NOT DRIVE YOUR VEHICLE INTO AREAS WHERE THE WATER COVERS THE\nROADWAY. THE WATER DEPTH MAY BE TOO GREAT TO ALLOW YOUR CAR TO CROSS\nSAFELY.",
      senderName  => "NWS Midland-Odessa (Western Texas and Southeastern New Mexico)",
      severity    => "Moderate",
      urgency     => "Expected",
    },
  },
}



回答2:


Ok, I was able to piece something together which worked:

use Weather::NOAA::Alert;
use Data::Dumper;

$alert = Weather::NOAA::Alert->new(['TXC301']);


$events = $alert->get_events();
$alert->poll_events();

Dumper($events);

print +( values %{ $events->{TXC301} } )[0]{expires}, "\n";

By changing $VAR1 to $events in the last line, I got rid of the error and got the exact output I needed. Thanks for all the quick replies!

Regards, Joseph Spenner



来源:https://stackoverflow.com/questions/26067109/perl-datadumper-to-extract-key-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!