Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

XMLin : Optimal Way to tell the difference between Array vs Scalar

by tptass (Sexton)
on Aug 19, 2008 at 23:08 UTC ( #705363=perlquestion: print w/replies, xml ) Need Help??

tptass has asked for the wisdom of the Perl Monks concerning the following question:

Let me preface this with this is actually working code and that I'm simply just looking for a better way to go about doing the follow, really the optimal way of do it.

I basically am reading an xml file and for a given key I'm creating anArray. However, there are cases where the key can have many xml elements in which xmlin returns an Array or the other case where just a single element (a scalar) is returned. Is there any other way to approach this, a better way?

if ( ref($xmlin->{$key}) eq "ARRAY") { @anArray = @{$xmlin->{$key}}; } else { push @anArray, $xmlin->{$key}; }
  • Comment on XMLin : Optimal Way to tell the difference between Array vs Scalar
  • Download Code

Replies are listed 'Best First'.
Re: XMLin : Optimal Way to tell the difference between Array vs Scalar
by BrowserUk (Patriarch) on Aug 19, 2008 at 23:48 UTC

      Thanks BrowserUK, I somehow missed that entire section on ForceArray and ForceContent. Need to look a little hard next time before posting. Thanks for the Help!

Re: XMLin : Optimal Way to tell the difference between Array vs Scalar
by ysth (Canon) on Aug 20, 2008 at 03:09 UTC
    I don't like that code. It should either push in both cases or assign in both cases. It also looks like a good candidate for the ternary operator, if ForceArray isn't an option.
    @anArray = ref $xmlin->{$key} ? @{$xmlin->{$key}} : $xmlin->{$key};
      @anArray = ref $xmlin->{$key} ? @{$xmlin->{$key}} : $xmlin->{$key};
      … which, since we're going through the keys in a hash, can be nicely combined with a map:
      @anArray = map { my $x; ref( $x = $xmlin->{$_} ) eq 'ARRAY' ? @$x : $x + } keys %$xmlin;
      (assuming that we want everything from the hash, not just a single entry—which is what I guess the poster wanted, which would explain the strange assignment/push choice).
Re: XMLin : Optimal Way to tell the difference between Array vs Scalar
by grantm (Parson) on Aug 21, 2008 at 01:01 UTC

    If you're going to use XML::Simple, you should read this node on how to use it responsibly (it also provides a solution to your original problem).

    Probably also worth looking at this node for weaning yourself off XML::Simple and onto something better.

      Talking about other options, this node might also be worth a read. It would require less changes from the code using XML::Simple, yet if needed can be turned into a stream-based parser handling the document in as small pieces as required.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2023-12-05 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (27 votes). Check out past polls.

    Notices?