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

Pretty Print Dumper for Hash of Array

by Anonymous Monk
on Sep 20, 2005 at 06:35 UTC ( #493372=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Respectable Monks,
Actually, This is a continuation of my earlier posting. Now with this code,
perl -MData::Dumper -e ' $Data::Dumper::Indent = 0; $hoa = {a => [1,2], 'b'=>[1,3], c=> [2,0]}; print Dumper $hoa; print "\n";'
How can I modify it to create:
$VAR1 = { 'c' => [2,0], 'a' => [1,2], 'b' => [1,3] };
Basically my intention is to make the indentation=0 only apply for the array. Thanks.

Replies are listed 'Best First'.
Re: Pretty Print Dumper for Hash of Array
by svenXY (Deacon) on Sep 20, 2005 at 07:19 UTC
    as far as I can see it in the manpages for Data::Dumper, it is not possible to do it like this.
    You might have to write your own pretty-printer for that. Something along the lines of:
    #!/usr/bin/perl use strict; use warnings; my $hoa = {a => [1,2], 'b'=>[1,3], c=> [2,0]}; foreach (keys %{$hoa}) { print "\t'$_' => [" . join(',', @{$hoa->{$_}}) . "],\n"; }
    'c' => [2,0], 'a' => [1,2], 'b' => [1,3],
Re: Pretty Print Dumper for Hash of Array
by g0n (Priest) on Sep 20, 2005 at 09:12 UTC
    if this is closer to what you want:

    $HASH1 = { a => [ 1, 2 ], b => [ 1, 3 ], c => [ 2, 0 ] };

    I used Data::Dump::Streamer to output that, with the following code:

    use Data::Dump::Streamer; $hoa = {a => [1,2], 'b'=>[1,3], c=> [2,0]}; print Dump($hoa); print "\n";


    $perlquestion=~s/Can I/How do I/g;

Re: Pretty Print Dumper for Hash of Array
by davidrw (Prior) on Sep 20, 2005 at 12:46 UTC
    You can (safely i think) clean the Data::Dumper output with regex's .. this is pretty close to what you want (might be easier though to split $s into an @lines array so that the first and last lines weren't touched by the substitutions...) :
    use Data::Dumper; use strict; use warnings; my $hoa = {a => [1,2], 'b'=>[1,3], c=> [2,0]}; my $s = Dumper $hoa; $s =~ s/^\s+//mg; $s =~ s/(?<!(\],| {))[\r\n]//sg; print $s; print "\n";
Re: Pretty Print Dumper for Hash of Array
by philcrow (Priest) on Sep 20, 2005 at 12:59 UTC
    You could use Data::HTMLDumper. I know that sounds odd, but it uses a grammar to parse Data::Dumper output. Instead of hard coding actions, it calls methods on an object whenever a production matches. By making the proper class to use in place of Data::HTMLDumper::Output, you can get pretty much any output you like. I like the module, but then I wrote it.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2016-09-30 00:52 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (562 votes). Check out past polls.