Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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)
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?
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.

    --
    <http://dave.org.uk>

    "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 :-)

        --
        <http://dave.org.uk>

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2014-07-23 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (146 votes), past polls