clintonm9 has asked for the wisdom of the Perl Monks concerning the following question:
then it could send me back all the results for that locations. In the data below it would send back PetA and PetB. Notice the different structure between PersonA and PersonB. If I sent Root->PersonB->Pets I would get back PetAA and PetBB without having to change anything. In the example below I convert everything to xml then use the XMLPAth to find the results. I do not think this is the best way but not sure how else to do it. Any ideas?my @return = &sub('Root->PersonA->Pets');
use Data::Dumper; use XML::Simple; use XML::XPath; use XML::XPath::XMLParser; my $perl_datastructure = { Root => { 'PersonA' => { Pets => [ PetA, PetB, ], }, 'PersonB' => [ { Pets => PetAA, }, { Pets => PetBB, }, ], }, }; print Dumper($perl_datastructure); $xmlstring = XMLout($perl_datastructure, NoAttr => 1, KeepRoot => +1); #print $xmlstring; my $xp = XML::XPath->new( xml => $xmlstring ); my $nodeset = $xp->find("//Root/PersonB/Pets"); foreach my $node ($nodeset->get_nodelist) { my $perldata = XMLin( XML::XPath::XMLParser::as_string($node) +); print $perldata, "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Hash Ref
by jethro (Monsignor) on Nov 19, 2009 at 04:06 UTC | |
by clintonm9 (Sexton) on Nov 20, 2009 at 13:43 UTC | |
|
Re: Perl Hash Ref
by ikegami (Patriarch) on Nov 19, 2009 at 04:02 UTC | |
|
Re: Perl Hash Ref
by ikegami (Patriarch) on Nov 19, 2009 at 03:57 UTC |