Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I find XML::Simple to be very simple if the XML is very simple, but with non-trivial XML I am often surprised by what it produces and, if I persist with it, end up spending a lot of time re-reading the documentation and studying Data::Dumper dumps.

Here is an example which shows, step by step as I worked it out, how to access the source_name element...

use strict; use warnings; use Data::Dumper; my $VAR1 = { 'sas_residue_annotation' => [ { 'xmlns' => 'http://www.ebi.ac.uk/WSsas/Schema', 'sources' => [ { 'source' => [ { 'source_name' => [ '1iho' ], 'ref_evalue' => [ '5.5e-50' ], 'ref_overlap' => [ '282' ], 'ref_identity' => [ '47.20' ], 'ref_pmid' => [ '11377204' ] }, { 'source_name' => [ '1mop' ], 'ref_evalue' => [ '8.3e-38' ], 'ref_overlap' => [ '264' ], 'ref_identity' => [ '43.60' ], 'ref_pmid' => [ '12717031' ] }, { 'source_name' => [ '1n2b' ], 'ref_evalue' => [ '8.2e-38' ], 'ref_overlap' => [ '264' ], 'ref_identity' => [ '43.60' ], 'ref_pmid' => [ '12717031' ] }, ], }, ], }, ], }; print Dumper($VAR1); print Dumper($VAR1->{sas_residue_annotation}); print Dumper($VAR1->{sas_residue_annotation}[0]); print Dumper($VAR1->{sas_residue_annotation}[0]{sources}); print Dumper($VAR1->{sas_residue_annotation}[0]{sources}[0]); print Dumper($VAR1->{sas_residue_annotation}[0]{sources}[0]{source}); foreach my $source (@{$VAR1->{sas_residue_annotation}[0]{sources}[0]{s +ource}}) { print "source name: ", join(',',@{$source->{source_name}}), "\n"; }

Note that I wrote each successive print after studying the output of the previous one. It is a bit tedious, but for deeply nested structures I find it faster than trying to do it all in my head and then debugging my errors.


In reply to Re^3: request for help on working with XML::Simple by ig
in thread request for help on working with XML::Simple by Angharad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found