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

TreePP (Hash or Array)

by bitman (Beadle)
on Jan 15, 2007 at 15:28 UTC ( #594758=perlquestion: print w/ replies, xml ) Need Help??
bitman has asked for the wisdom of the Perl Monks concerning the following question:

TreePP is returning an array of hashes to me if there's more than one record, but just a hash if there's only one record. Anyone have some nifty code to handle this with elegance.

Comment on TreePP (Hash or Array)
Replies are listed 'Best First'.
Re: TreePP (Hash or Array)
by davorg (Chancellor) on Jan 15, 2007 at 15:36 UTC

    You write as though you assume that everyone should just know what "TreePP" is. I think you probably mean XML::TreePP.

    If that's the case then perhaps the force_array option will be useful.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thank you, You did know though. I knew you would. Many thanks for that.
        You did know though

        I didn't know. But Google helped me to make an intelligent guess. You shouldn't rely on people being bored enough to go out of their way to help you :-)


        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: TreePP (Hash or Array)
by eff_i_g (Curate) on Jan 15, 2007 at 15:34 UTC
    Are you talking about XML::TreePP? You can use ref to check the return type: if it's an array, OK; if it's a hash, put it into an array; continue processing.

    You may want to contact the author to see if this could be handled better in the module, or perhaps there is reason behind the behavior.

    Update: Kudos to davorg for reading the docs :)
      Except, it didn't work. But I used your solution, but I guess my code isn't that elegant though.
      if ( ref $tree->{$xfile}->{$rec} eq 'HASH' ) { my $x=$tree->{$xfile}->{$rec}; delete $tree->{$xfile}->{$rec}; $tree->{$xfile}->{$rec}[0]=($x); }
        force_array did not work? Is it not what it seems to be, a bug, or, upon second glance, a (gasp!) user error?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://594758]
Approved by eff_i_g
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2016-05-29 21:19 GMT
Find Nodes?
    Voting Booth?