in reply to hashes, arrays, and references... oh my
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.
|
|---|