Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

zing:

It's not enough to simply include Data::Dumper, you actually have to call it. Add print Dumper(\%HoA),"\n"; to the end of your script to let it help you out.

The slightly longer answer is this: If you want to display the structure of the data, you have to treat different data items differently. So you'd have to create a subroutine that would look at the data and handle each type differently, something like:

sub print_thing { print join("\n", _thing_helper(shift)),"\n"; } sub _thing_helper { # return a list of strings representing the thing we're given my $thing = shift; # ok, what is it? if (ref $thing eq "ARRAY") { return "array [", # Turn each thing in the list into a string map( { _thing_helper($_) } @$thing ), "]"; } elsif (ref $thing eq "HASH") { return "hash {", # Turn each thing in the array into a string map( { "$_ =>" . _thing_helper($$thing{$_}) } keys %$thing) +, "}"; else { # A good solution would also do something with objects... return "$thing"; } }

But after writing something like this, you'll probably only wind up with your own customer version of Data::Dumper anyway. But the point is that when you print a string or number, perl knows what you want: it prints the text representation of the value. For something that's a reference to some other thing, though, there are too many different ways to format the data for perl to pick out a good one for you. So it settles for telling you *what* the thing is, and a token that lets you tell whether it's the same item as another one you printed. There are different ways to display arrays, hashes and objects, so there are different packages to handle them.

Notes:

  • The code is untested and probably wrong, as I composed it in the PM textbox.
  • I glossed over some things, so you should refer to the documentation for the details.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Problem printing/storing hash by roboticus
in thread Problem printing/storing hash by zing

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 perusing the Monastery: (7)
As of 2024-04-16 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found