Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello. This is my first question so my apologies in advance if I break any taboos. I did read the formatting tips and how to ask a question articles.

From a high level I am trying to write an XML parser. I'm using XML::Simple to get the XML into a hash and am then navigating that hash to pull out the data I need. In the hash that XML::Simple gives me, there is an array of hashes, and while looping through those I have a problem with the structure below:

'user' => { + 'firstname' => 'Joe', + 'lastname' => 'Shmoe', }, 'user' => 'Finance Department'

What I want to do is print out either the user's full name OR the department if there is no substructure under the user reference. However, I get a "Can't use string ("Vox Procurement Managers") as a HASH ref while "strict refs" in use at line 138." error when the code gets to the Finance Department bit. I've tried checking if(exists $xml->$usernumber->{"user"}->{"firstname"} before trying to print the full name but that still throws the error. Further googling and searching here has not helped. Basically I need a way to check if that reference in the hash points to a string ("Finance Department") or to more hashes down below. An if statement based on if( ref ($xml->$usernumber->{"user"}}) eq "HASH") always returned true and did not work either.

In reply to Parsing hashes with variable structure by SomeNoob

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the sunlight beams...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2018-03-24 14:05 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (298 votes). Check out past polls.