I'm very lost. I am trying to do the following:

I have an XML file from Nagios. Here's the structure:

1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 <NAGIOS> 3 <DATASOURCE> 4 <TEMPLATE>check_bigip_pool_connection</TEMPLATE> 5 <IS_MULTI>0</IS_MULTI> 6 <DS>1</DS> 7 <NAME>cur</NAME> 8 <UNIT></UNIT> 9 <ACT>2198</ACT> 10 <WARN></WARN> 11 <WARN_MIN></WARN_MIN> 12 <WARN_MAX></WARN_MAX> 13 <WARN_RANGE_TYPE></WARN_RANGE_TYPE> 14 <CRIT></CRIT> 15 <CRIT_MIN></CRIT_MIN> 16 <CRIT_MAX></CRIT_MAX> 17 <CRIT_RANGE_TYPE></CRIT_RANGE_TYPE> 18 <MIN></MIN> 19 <MAX></MAX> 20 </DATASOURCE> 21 <DATASOURCE> 22 <TEMPLATE>check_bigip_pool_connection</TEMPLATE> 23 <IS_MULTI>0</IS_MULTI> 24 <DS>2</DS> 25 <NAME>max</NAME> 26 <UNIT></UNIT> 27 <ACT>5400</ACT> 28 <WARN></WARN> 29 <WARN_MIN></WARN_MIN> 30 <WARN_MAX></WARN_MAX> 31 <WARN_RANGE_TYPE></WARN_RANGE_TYPE> 32 <CRIT></CRIT> 33 <CRIT_MIN></CRIT_MIN> 34 <CRIT_MAX></CRIT_MAX> 35 <CRIT_RANGE_TYPE></CRIT_RANGE_TYPE> 36 <MIN></MIN> 37 <MAX></MAX> 38 </DATASOURCE> ...

Much more follows. The above section is the part I care about. I want to get data from the <ACT> tags associated with the <NAME> "cur".

I'm reading it in with XML::Simple with the following code:

1 #!/usr/bin/env perl 2 3 4 use Data::Dumper; 5 use XML::Simple; ... 9 my $xs1 = XML::Simple->new(); 10 my $file = $ARGV[0]; 11 my $doc = $xs1->XMLin($file, forcearray => 1); 12

When I use Dumper($datasource), I get this:

$VAR1 = { 'NAGIOS_NOTIFICATIONRECIPIENTS' => {}, ... 'NAGIOS_HOSTACTIONURL' => '/nagios/pnp/index.php?host=ahostn +ame.somedomain.com', 'DATASOURCE' => [ { 'MIN' => {}, 'TEMPLATE' => 'check_bigip_pool_connection +', 'CRIT_MAX' => {}, 'MAX' => {}, 'UNIT' => {}, 'WARN_MAX' => {}, 'NAME' => 'cur', 'WARN_MIN' => {}, 'IS_MULTI' => '0', 'DS' => '1', 'WARN' => {}, 'CRIT_MIN' => {}, 'ACT' => '2198', 'CRIT' => {}, 'CRIT_RANGE_TYPE' => {}, 'WARN_RANGE_TYPE' => {} }, ...

Next, I ran this:

20 foreach my $datasource ($doc->{DATASOURCE}){ 21 22 print Dumper($datasource->[1]->{NAME}->[0]); 23 24 my $element = scalar(@datasource); 25 26 print "Element = $element\n"; 27 for(my $element=0; $element < scalar(@datasource); $element++){ 28 print $datasource[$element]; 29 } 30 }

and as far as I understand, datasource should be an array of hashes. But $element is 0, before I use it in the for loop. Output:

$VAR1 = 'max'; Element = 0

Now here's the weird part where I decided to ask for help. When I change the print statement to

 22   print Dumper($datasource[1]->{NAME}->[0]);

(notice the lack of '->' after $datasource) it changes my output to

$VAR1 = undef; Element = 2 HASH(0x10e2ef0)

  1. Why would changing the print statement change my output? Am I doing an assignment operation and I just don't realize it?
  2. How do I get the ACT value for all DATASOURCEs with the NAME "cur"?
Any help would be appreciated.

Thank You.


In reply to Strange Behavior with XML::Simple when pulling info out of an XML file by lunchlady55

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.