Hello all!
I'm pretty new to perl, and am currently trying to throw together a script that will parse some XML for me. Here's a sample of what's giving me trouble:
use strict;
use LWP;
use XML::Simple;
use Data::Dumper;
my $var_lat = "45.96968";
my $var_lon = "-93.33434899999997";
my $ua = LWP::UserAgent->new;
my $forecast_xml = $ua->request(HTTP::Request->new(GET => "http://fore
+cast.weather.gov/MapClick.php?lat=$var_lat&lon=$var_lon&unit=0&lg=eng
+lish&FcstType=dwml"));
my $forecast_arr = XMLin($forecast_xml->content);
foreach my $item (@{$forecast_arr->{'data'}->[0]->{'time-layout'}}) {
print Dumper $item;
}
If run, you'll see what I'm dealing with - a hash which contains hashes, all of which have the same name (VAR1) but contain different data.
My goal is to extract each period-name to a separate variable (ie. $name0="Today", $name1="Tonight", $name2="Tuesday", etc). I'm only interested in the period names associated with layout-key 'k-p12h-n13-1', however; the rest are inconsequential.
Trouble is, I'm not sure how best to do that. I tried simply picking out the correct hash numerically, but the incoming XML frequently changes the order. The data in the first hash now might be in the third hash later today.
How can I "single out" just the hash associated with layout-key 'k-p12h-n13-1', and get the needed data out?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.