Hello:

I thought I had this figured out once before, but now it's not coming to me (or at least not quit right). I always seem to have troubles with the whole hash/array thing as my brain always seems to interpret it differently than it is.

I have a XML file, some of the data entries have 'codeshares' elements, and some do not. Some have only 1 and others may have 2 or 3.

I am using XML::Simple because is works for 98% of what I need.

I have tried using the following in hopes to get a number response back based on the number of codeshares entries:

  my $codeshares = scalar @{$database->{'flightStatuses'}->{'flightStatus'}->[$loop]->{'codeshares'}->{'codeshare'}};

And a XML Sample:

<flightStatuses> <flightStatus> <flightId>292085149</flightId> <carrierFsCode>MQ</carrierFsCode> <flightNumber>4070</flightNumber> <codeshares> <codeshare> <fsCode>AA</fsCode> <flightNumber>4070</flightNumber> <relationship>X</relationship> </codeshare> <codeshare> <fsCode>BA</fsCode> <flightNumber>4841</flightNumber> <relationship>Z</relationship> </codeshare> </codeshares> </flightStatus> </flightStatuses>

But if it is less than 2 codeshares, I get "Not an ARRAY reference at..." back. If I have 2 or more codeshares it seems to work fine.

Anyone know what I might possibly be missing, or is there another way to find out (in this case) how may codeshares there are, so I can read them? Everything else I have so far works fine.

Thanks as always!

John

In reply to Check if XML element exists by johnfl68

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.