Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Beautiful recursive print

by chimiXchanga (Novice)
on Mar 17, 2017 at 09:38 UTC ( [id://1185001]=perlquestion: print w/replies, xml ) Need Help??

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

Hello guys, I'm practicing some algorithms, but I'm stuck on something. I wrote a recursive sub to deref basic references.
sub dumpomatic { my ($obj) = @_; if (ref($obj) eq 'ARRAY') { print "[ "; for my $index (0.. $#$obj) { dumpomatic($obj->[$index]); print ", " if $index < $#$obj; } print " ]"; } elsif (ref($obj) eq 'HASH') { my @keys = sort keys %$obj; print "{ "; for my $index (0.. $#keys) { print "$keys[$index] => "; dumpomatic($obj->{$keys[$index]}); print ", " if $index < $#keys; } print " }"; } elsif (ref($obj) eq 'SCALAR') { print $$obj; } else { print $obj; } }
I'm looking to alter the output, the way Data::Dumper prints it out. Any help on that? Thank you!

Replies are listed 'Best First'.
Re: Beautiful recursive print
by Corion (Patriarch) on Mar 17, 2017 at 09:44 UTC

    What part of the output as Data::Dumper creates it are you missing?

    If you want to nicely indent your code, pass the level as a second parameter to your routine when it recurses, and indent according to level:

    sub dumpomatic { my( $obj, $level ) = @_; $level ||= 0; my $prefix = " " x $level; ... print $prefix. "["; ... print $prefix. "{"; ... print $prefix. $$obj; ... print $prefix. $obj;

    Also, you might want to recurse once more in the ref($obj) eq 'SCALAR' case, and not print $$obj but '\\' and then dumpomatic($$obj).

    This would also handle pathologic cases like

    my $ar = ['pirate', 'ninja']; my $arr = \$ar; my $arrr = \$arr; dumpomatic( $arrr );
      I fixed the 'SCALAR' case, but I still miss the proper \n after each element {[$obj => ]}
Re: Beautiful recursive print
by huck (Prior) on Mar 17, 2017 at 10:16 UTC

    watch out for cyclic structures.

    use strict; use warnings; use Data::Dumper; no warnings 'once' ; local $Data::Dumper::Sortkeys=1; local $Data::Dumper::Indent=1; use warnings; my $c={}; $c->{'1top'}={name=>'1top',childs=>[],parent=>undef}; makechild(name=>'child1',parent=>'1top'); makechild(name=>'child2',parent=>'1top'); makechild(name=>'child21',parent=>'child2'); makechild(name=>'child22',parent=>'child2'); print Dumper($c); sub makechild{ my %args=@_; my $node={}; $c->{$args{name}}=$node; $node->{name}=$args{name}; $node->{childs}=[]; my $parent=$c->{$args{parent}}; $node->{parent}=$parent; push @{$parent->{childs}},$node; }
    I think Dumper does it with a 'global' hash of all places already visited.
    $VAR1 = { '1top' => { 'childs' => [ { 'childs' => [], 'name' => 'child1', 'parent' => $VAR1->{'1top'} }, { 'childs' => [ { 'childs' => [], 'name' => 'child21', 'parent' => $VAR1->{'1top'}{'childs'}[1] }, { 'childs' => [], 'name' => 'child22', 'parent' => $VAR1->{'1top'}{'childs'}[1] } ], 'name' => 'child2', 'parent' => $VAR1->{'1top'} } ], 'name' => '1top', 'parent' => undef }, 'child1' => $VAR1->{'1top'}{'childs'}[0], 'child2' => $VAR1->{'1top'}{'childs'}[1], 'child21' => $VAR1->{'1top'}{'childs'}[1]{'childs'}[0], 'child22' => $VAR1->{'1top'}{'childs'}[1]{'childs'}[1] };

      Recursive data structures are tricky. In fact, it appears that even Dump does not always get it exactly right. (Refer to Choroba's answer to my my question Marshalling Data.) Sorry, I was to lazy to write the bug report that he recommended.
      Bill
        I did, but it seems the bug won't be fixed.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Beautiful recursive print
by Eily (Monsignor) on Mar 17, 2017 at 10:37 UTC

    FYI, Data::Dump comes with Data::Dump::Filtered for which you can provide a callback that will be called on each element to be dumped. With the callback you can replace the output string entirely (eg: replace an int value with a constant, or display it in hexadecimal form), add a comment to be printed with the data, ignore some keys from a hash etc...

    If it's the formatting you're after maybe perltidy can help you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-23 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found