I am trying to get all object oriented with my latest 'little' perl script. to do this, I am parsing some xml, looking for different object types. When I find the object name, I instantiate the object and in the constructor, I look for the various tags in each object, and then store them in the $self array hash. So far, so good. Here is a sample object:
<ObjectType> <AppObject>hello</AppObject> <AppObjectField>gender</AppObjectField> <valueTargetPair value="MALE" targetPo="Incoming 1" /> <valueTargetPair value="FEMALE" targetPo="Incoming 2" /> </ObjectType>
I do things like: ...
}elsif ($_ =~ /<AppObjectField>(.*)<\/AppObjectField>/) { $self->{appObjFld} = $1; }
... no problem. my problem is the more complicated <valueTargetPair> tag. I parse the values for the value and target labels into $1 and $2. I want to put these two things into a hash with two entries, having value and targetPo as the keys. Now, because I have several valueTargetPair tags, I need to put this has into an array! I do all of that like this:
if ($inline =~ /<valueTargetPair (.*)\/>/) { if ($inline =~ /value=\"(.*?)\" targetPo=\"(.*?)\"/) { my %theHash = {value=>$1, targetPo=>$2}; push (@arry, \%theHash); } }
then, to keep it in my $self object, I do something like:
$self->{valuePair} = \@arry;
To summarize, I parse some xml (not really important that it is xml), and put some of the results into a hash. I store the hashes in an array, which I then need to store in another hash! This leaves me with a hash holding an array holding hashes. I think that the above is all okish My problem, is that later on, I run a method to print out the contents. pulling the simple values (like $self->{appObjFld} ) out of the $self hash is easy. getting that value Pair stuff out is murder. How can I pull out the original %theHash, so that I can print each valueTargetPair?


...regan

In reply to hashes, arrays, and references... oh my by regan

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.