So you're reading xml data that is created by some other process that you wrote, and you know what to expect. XML parsers are good, and I commend their use, but in your case, I wouldn't fault the use of regexes to parse it.

To get back to your original question, which is a data structure problem, a regex solution sort of like the following ought to work (and of course, filling the data structure via an official XML parser module should work in a similar way). I put this into a simple-minded harness to test it out -- you'll need to complicate it a bit, I'm sure.

#!/usr/bin/perl use strict; use Data::Dumper; my %hash; my $self = \%hash; while (<DATA>) { if ( m{<(\w+)>([^<]+)</\1>} ) { # a content element $$self{$1} = $2; # store the content keyed by tag name } elsif ( m{<(\w+) ([^>]+)/>} ) { # an "empty" element my ($elem,$attr) = ($1,$2); # get tag name and attributes my %attrhash; while ( $attr =~ s/(\w+)="([^"]+)"// ) { $attrhash{$1} = $2; } push @{$$self{$elem}}, \%attrhash; # expect multiple instances } } print Dumper( $self ); # here's one way to access the structure's contents: for my $tag ( sort keys %$self ) { if ( ref( $$self{$tag} ) eq 'ARRAY' ) { for ( @{$$self{$tag}} ) { # $_ is now an array element contain +ing a hash ref for my $attr ( sort keys %{$_} ) { print "$tag : $attr = $$_{$attr}\n"; } } } else { # the tag had a plain string as content print "$tag = $$self{$tag}\n"; } } __DATA__ <ObjectType> <AppObject>hello</AppObject> <AppObjectField>gender</AppObjectField> <valueTargetPair value="MALE" targetPo="Incoming 1" /> <valueTargetPair value="FEMALE" targetPo="Incoming 2" /> </ObjectType>

You'll want to study up on the "perlref" (and "perlreftut") man pages, if you haven't done that yet.

update: jeffa's solution is of course the correct one -- having studied that (as I should have done sooner), I conclude that my own proposed code is unnecessarily complicated and obscure. Do be sure to notice that jeffa solved the problem you were having with references.


In reply to Re: hashes, arrays, and references... oh my by graff
in thread 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.