1

我编写了一些 perl 代码,用于从 NOAA 获取天气数据/警报。

我的代码很简单:

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";

输出:

$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

TXC301 是一个报告标识符。

脚本的输出将打印从 NOAA 获取的所有值。

目标是存储/返回“过期”值。

我已经注释掉了几行,这是为了实现我的目标的尝试。

我遇到的问题是获取 $url 变量。我需要这个值才能获得我的 $expires 值。我的代码中倒数第二行将正确获取 $expires 值,但为了做到这一点,我需要将 URL 硬编码到该行中。

我正在尝试使该线(倒数第三个)正上方的线起作用:

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

但这取决于要存储的 $url 值。我似乎无法弄清楚如何获取 $url 值。

我的猜测:

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

这些都不起作用。

任何帮助都会很棒。

谢谢!

问候,

约瑟夫·斯宾纳

4

2 回答 2

2

鉴于该级别的哈希引用只有一个值,您可以使用values

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

输出:

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

替代数据::Dumper

另外,在一个单独的问题上,我想推荐使用Data::Dumpover the core library Data::Dumper

此替代方案的默认设置和功能可提供出色的输出,并使人们能够更快地分析数据结构,如下所示:

$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;

输出:

{
  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",
    },
  },
}
于 2014-09-26T19:46:05.920 回答
-1

好的,我能够拼凑出一些有用的东西:

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";

通过在最后一行更改$VAR1$events,我摆脱了错误并获得了我需要的确切输出。感谢所有快速回复!

问候,约瑟夫·斯宾纳

于 2014-09-26T20:11:56.750 回答