Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
One element could either have a single value or an array of values.

I agree with Your Mother, sounds like you might be using XML::Simple. I have to strongly recommend against this module (as its own documentation does, see the section "Status of this Module"), except for maybe the very simplest of cases, of which this is probably not one. Instead, look into other XML parsing modules, such as XML::LibXML, XML::Twig, or XML::Rules, which can output data structures much like XML::Simple:

use warnings; use strict; use Data::Dump; use XML::Rules (); my $XML1 = '<root><phone><callTo>X</callTo></phone></root>'; my $XML2 = '<root><phone><callTo>Y</callTo> <callTo>Z</callTo></phone></root>'; my $parser = XML::Rules->new( stripspaces => 3|4, rules => [ root => 'pass', phone => 'as is', callTo => 'content array', _default => sub { die "Unknown tag $_[0]" }, ] ); dd $parser->parse($XML1); dd $parser->parse($XML2); __END__ { phone => { callTo => ["X"] } } { phone => { callTo => ["Y", "Z"] } }

As for XML::Simple, note its ForceArray option:

use XML::Simple qw/:strict XMLin/; dd XMLin($XML1, KeyAttr=>{}, ForceArray=>[]); dd XMLin($XML2, KeyAttr=>{}, ForceArray=>[]); dd XMLin($XML1, KeyAttr=>{}, ForceArray=>['callTo']); __END__ { phone => { callTo => "X" } } { phone => { callTo => ["Y", "Z"] } } { phone => { callTo => ["X"] } }

Lastly, to answer your original question, if you have a scalar that holds either a single value or an array reference, one way to handle that is like the following. However, it should be clear from the above that this is not the best way to fix your problem.

foo( "one" ); foo( ["two", "three"] ); sub foo { my $x = shift; # the following does the trick my @values = ref $x eq 'ARRAY' ? @{$x} : $x; for my $val (@values) { print "<$val>\n"; } } __END__ <one> <two> <three>

Update: Refactored last code example slightly to make the solution more clear. Update 2: Used callTo => 'content array' instead of 'as array' to make XML::Rules output identical to XML::Simple.


In reply to Re: What is correct way to reference? by haukex
in thread What is correct way to reference? by BradV

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 about the Monastery: (4)
As of 2024-04-23 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found