Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

TreePP (Hash or Array)

by bitman (Scribe)
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.

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2018-03-20 02:29 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.