Joseph
Joseph

Reputation: 121

Accessing variables returned from perl: Dumper

I'm having problems accessing elements/arrays returned from perl Data::Dumper. I'm able to access results such as these:

$VAR1 = {
      'head' => {
                'source' => {
                            'credit-logo' => 'foo',
                            'feedback' => 'bar',
                            'more-information' => 'google',

And with the following line, I can get the 'credit-logo' value:

$credit=        $result->{head}->{'source'}->{'credit-logo'};

However, I'm having trouble with the following, where the whole thing starts with a "[" instead of a "{":

$VAR1 = [
      'dwml',
      {
        'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
        'version' => '1.0',
        'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
        'xsi:noNamespaceSchemaLocation' => 'site'
      },
      [
        [
          'head',
          {},
          [
            [
              'product',
              {
                'concise-name' => 'time-series',
                'srsName' => 'WGS 1984',
                'operational-mode' => 'official'

In the above, how would I access the 'srsName' value?

No matter what I try, I get messages about the value not being a HASH or ARRAY.

-- My original script is trying to get forecast data from NWS:

#!/usr/bin/perl

use NDFD::Weather::Report;
use Data::Dumper;


$result=NDFD::Weather::Processer->new(

    listLatLon=>'30.3369,-81.6614',
    product=>'time-series',
    Unit=>'m',
    maxt =>0,
    mint =>0,
    temp =>1,
    dew =>1,
    pop12 =>0,
    qpf =>0,
    sky =>1,
    snow =>0,
    wspd =>1,
    wdir =>1,
    wx =>1,
    waveh =>0,
    icons =>0,
    rh =>0,
    appt =>0,
    incw34 =>0,
    incw50 =>0,
    incw64 =>0,
    cumw34 =>0,
    cumw50 =>0,
    cumw64 =>0,
    conhazo =>0,
    ptornado =>0,
    phail =>0,
    ptstmwinds =>0,
    pxtornado =>0,
    pxhail =>0,
    pxtstmwinds =>0,
    ptotsvrtstm =>0,
    pxtotsvrtstm =>0,
    tmpabv14d =>0,
    tmpblw14d =>0,
    tmpabv30d =>0,
    tmpblw30d =>0,
    tmpabv90d =>0,
    tmpblw90d =>0,
    prcpabv14d =>0,
    prcpblw14d =>0,
    prcpabv30d =>0,
    prcpblw30d =>0,
    prcpabv90d =>0,
    prcpblw90d =>0,
    precipa_r =>0,
    sky_r =>0,
    td_r =>0,
    temp_r =>0,
    wdir_r =>0,
    wspd_r =>0,
    wgust =>0

);
$value= $result->NDFDgenLatLonList;
$xml_handler1=$result->Parse_XML_Data_decode($value);


print Dumper $xml_handler1;

My goal is to extract the values returned from that Dumper response.

Here is the XML returned, by adding: print $value;

--

<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
<head>
<product srsName="WGS 1984" concise-name="time-series" operational-mode="official">
  <title>NOAA's National Weather Service Forecast Data</title>
  <field>meteorological</field>
  <category>forecast</category>
  <creation-date refresh-frequency="PT1H">2015-10-02T15:56:10Z</creation-date>
 </product>
 <source>
  <more-information>http://www.nws.noaa.gov/forecasts/xml/</more-information>
  <production-center>Meteorological Development Laboratory<sub-center>Product Generation Branch</sub-center></production-center>
  <disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
  <credit>http://www.weather.gov/</credit>
  <credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
  <feedback>http://www.weather.gov/feedback.php</feedback>
</source>
</head>
<data>
<location>
  <location-key>point1</location-key>
  <point latitude="30.34" longitude="-81.66"/>
</location>
<moreWeatherInformation applicable- >
<time-layout time-coordinate="local" summarization="none">
  <layout-key>k-p3h-n35-1</layout-key>
  <start-valid-time>2015-10-02T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-02T17:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-02T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-02T23:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T05:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T11:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T17:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T23:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T05:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T11:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T17:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-05T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-05T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-05T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-05T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-06T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-06T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-06T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-06T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-07T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-07T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-07T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-07T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-08T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-08T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-08T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-08T20:00:00-04:00</start-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="none">
  <layout-key>k-p3h-n19-2</layout-key>
  <start-valid-time>2015-10-02T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-02T17:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-02T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-02T23:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T05:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T11:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T17:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T20:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-03T23:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T02:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T05:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T08:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T11:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T14:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T17:00:00-04:00</start-valid-time>
  <start-valid-time>2015-10-04T20:00:00-04:00</start-valid-time>
</time-layout>
<parameters applicable-location="point1">
  <temperature type="hourly" units="Celsius" time-layout="k-p3h-n35-1">
    <name>Temperature</name>
    <value>23</value>
    <value>23</value>
    <value>21</value>
    <value>20</value>
    <value>19</value>
    <value>18</value>
    <value>18</value>
    <value>23</value>
    <value>26</value>
    <value>25</value>
    <value>22</value>
    <value>21</value>
    <value>20</value>
    <value>19</value>
    <value>19</value>
    <value>24</value>
    <value>26</value>
    <value>26</value>
    <value>22</value>
    <value>19</value>
    <value>18</value>
    <value>27</value>
    <value>23</value>
    <value>21</value>
    <value>19</value>
    <value>27</value>
    <value>23</value>
    <value>21</value>
    <value>20</value>
    <value>27</value>
    <value>24</value>
    <value>22</value>
    <value>21</value>
    <value>28</value>
    <value>24</value>
  </temperature>
  <temperature type="dew point" units="Celsius" time-layout="k-p3h-n35-1">
    <name>Dew Point Temperature</name>
    <value>20</value>
    <value>20</value>
    <value>19</value>
    <value>18</value>
    <value>17</value>
    <value>17</value>
    <value>17</value>
    <value>17</value>
    <value>18</value>
    <value>18</value>
    <value>18</value>
    <value>17</value>
    <value>17</value>
    <value>17</value>
    <value>17</value>
    <value>17</value>
    <value>17</value>
    <value>18</value>
    <value>18</value>
    <value>18</value>
    <value>18</value>
    <value>19</value>
    <value>19</value>
    <value>18</value>
    <value>18</value>
    <value>19</value>
    <value>18</value>
    <value>18</value>
    <value>18</value>
    <value>19</value>
    <value>19</value>
    <value>19</value>
    <value>19</value>
    <value>19</value>
    <value>20</value>
  </temperature>
  <wind-speed type="sustained" units="meters/second" time-layout="k-p3h-n35-1">
    <name>Wind Speed</name>
    <value>5</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>5</value>
    <value>5</value>
    <value>5</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>3</value>
    <value>2</value>
    <value>3</value>
    <value>3</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>5</value>
    <value>4</value>
    <value>3</value>
    <value>4</value>
    <value>4</value>
    <value>4</value>
    <value>3</value>
    <value>4</value>
    <value>5</value>
    <value>4</value>
  </wind-speed>
  <direction type="wind" units="degrees true" time-layout="k-p3h-n35-1">
    <name>Wind Direction</name>
    <value>0</value>
    <value>360</value>
    <value>340</value>
    <value>340</value>
    <value>320</value>
    <value>310</value>
    <value>300</value>
    <value>280</value>
    <value>280</value>
    <value>270</value>
    <value>280</value>
    <value>280</value>
    <value>270</value>
    <value>260</value>
    <value>240</value>
    <value>250</value>
    <value>250</value>
    <value>240</value>
    <value>240</value>
    <value>260</value>
    <value>300</value>
    <value>350</value>
    <value>360</value>
    <value>350</value>
    <value>350</value>
    <value>10</value>
    <value>10</value>
    <value>20</value>
    <value>10</value>
    <value>30</value>
    <value>40</value>
    <value>40</value>
    <value>40</value>
    <value>60</value>
    <value>60</value>
  </direction>
  <cloud-amount type="total" units="percent" time-layout="k-p3h-n35-1">
    <name>Cloud Cover Amount</name>
    <value>100</value>
    <value>100</value>
    <value>100</value>
    <value>99</value>
    <value>97</value>
    <value>90</value>
    <value>79</value>
    <value>71</value>
    <value>64</value>
    <value>72</value>
    <value>79</value>
    <value>78</value>
    <value>76</value>
    <value>72</value>
    <value>69</value>
    <value>69</value>
    <value>51</value>
    <value>51</value>
    <value>35</value>
    <value>28</value>
    <value>64</value>
    <value>63</value>
    <value>63</value>
    <value>62</value>
    <value>62</value>
    <value>60</value>
    <value>48</value>
    <value>37</value>
    <value>32</value>
    <value>35</value>
    <value>37</value>
    <value>41</value>
    <value>45</value>
    <value>40</value>
    <value>31</value>
  </cloud-amount>
  <wind-speed type="gust" units="meters/second" time-layout="k-p3h-n19-2">
    <name>Wind Speed Gust</name>
    <value>7</value>
    <value>6</value>
    <value>6</value>
    <value>6</value>
    <value>6</value>
    <value>6</value>
    <value>6</value>
    <value>7</value>
    <value>7</value>
    <value>7</value>
    <value>5</value>
    <value>6</value>
    <value>6</value>
    <value>6</value>
    <value>5</value>
    <value>5</value>
    <value>6</value>
    <value>6</value>
    <value>4</value>
  </wind-speed>
  <weather time-layout="k-p3h-n35-1">
    <name>Weather Type, Coverage, and Intensity</name>
    <weather-conditions>
      <value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
      <value coverage="slight chance" intensity="none" additive="and" weather-type="thunderstorms" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
      <value coverage="slight chance" intensity="none" additive="and" weather-type="thunderstorms" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
      <value coverage="slight chance" intensity="none" additive="and" weather-type="thunderstorms" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions>
      <value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
        <visibility xsi:nil="true"/>
      </value>
    </weather-conditions>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
    <weather-conditions/>
  </weather>
</parameters>

Upvotes: 2

Views: 575

Answers (2)

Sobrique
Sobrique

Reputation: 53478

You appear to be using XML::Parser->new(Style => 'Tree'). This parser's interface is rather simplistic, which is a nice way to say it's not very user-friendly. You can figure out how to do it by counting the brackets:

$VAR -> [2][0][2][0][1]{'srsName'}; 

But seriously, there are far friendlier options. Just use XML::Twig or XML::LibXML because then the above becomes:

 $twig -> findnodes('//srsName',0)->trimmed_text; 

(or something similar - like I say, give us some XML and we can give you an exact answer).

Edit:

Like this (NB: Untested, because I can't get that module to download):

 use XML::Twig; 

 my $value= $result->LatLonListCityNames;
 my $twig = XML::Twig -> parse ( $value ); 

 my $srsName_element = $twig -> findnodes ( '//srsName', 0 );
 $srsName_element -> print;
 print $srsName_element -> trimmed_text;  

Upvotes: 1

tjwrona
tjwrona

Reputation: 9035

The reason you are having trouble is because [ means that the data is contained in an array reference, not a hash reference, so it must be accessed differently.

Lets try and break this down (replacing all the contents with placeholders)

$VAR1 = [
          0 (string),
          1 (hash  ),
      --> 2 (array ) [
                   --> 0 (array) [
                                   0 (string),
                                   1 (hash  ),
                               --> 2 (array ) [
                                            --> 0 (array) [
                                                            0 (string )
                                                        --> 1 (hash   ) {
                                                                      --> 'srsName' => $val

Simply follow the -->'s. Each one points to the next element you must access to traverse the data structure.

I have not tested this, but it appears that it can be accessed through

my $srsName = $xml_handler1->[2][0][2][0][1]{'srsName'};

As many others have said though, using Data::Dumper and manually accessing the data is clearly not a good way to parse XML. I personally am not familiar with the "proper" methods you should use to do this, but I suggest you read the comments and take a look at Sobrique's answer.


On a side note... You should ALWAYS add use strict and use warnings to the top of your scripts and define all variables lexically using my. It prevents bad coding habits and can save you a lot of headaches trying to debug code with hard to find problems.

For a list of reasons why check out this article.

Upvotes: 0

Related Questions