Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: "Dynamically" Accessing a HoH

by ChemBoy (Priest)
on Jun 02, 2005 at 16:10 UTC ( [id://462958]=note: print w/replies, xml ) Need Help??


in reply to "Dynamically" Accessing a HoH

If I were a user, I'd be annoyed by that format for $usr_string, so I'm going to assume you're not tied to it, and can have them supply something (slightly) more intuitive like depTar/name instead.

Using the power of unjustified assumptions makes coding much easier, you see... <grin>

my @keys = split '/', $usr_string; my $ref = $xml; $ref = $ref->{$_} for @keys; print $ref;

Obviously, this completely fails to check for error conditions of any kind (invalid keys owing to typos being the most obvious case). I'm assuming you know how to do that already. :-)



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders

Replies are listed 'Best First'.
Re^2: "Dynamically" Accessing a HoH
by dave0 (Friar) on Jun 02, 2005 at 16:26 UTC
    Given that the input data was originally XML parsed with XML::Simple, if you're going to use a format like depTar/name for the user input, you can use XML::XPath to extract the results:
    use XML::XPath; use XML::XPath::XMLParser; my $usr_string = "depTar/name"; my $xml = "<depTar><name>c_p20</name><loc>srvr1</loc></depTar>"; my $xp = XML::XPath->new(xml => $xml); my $nodeset = $xp->find($usr_string); foreach my $node ($nodeset->get_nodelist) { print $node->string_value(), "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://462958]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found