Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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
[stevieb]: shmem thanks for the 'insight' :P
[shmem]: good thing that Sun already took "OpenWindows", otherwise I'd not stop to shudder imagining an "OpenWindows" from MS
[shmem]: more garbage in, more garbage out that would be
[stevieb]: I found that win10 broke a C# library I was using for one project while enhancing tests for a Perl dist, which breaks other Perl dists, and I'm about to throw my hands up on berrybrew. win2k12 broke one thing, win10 breaks something...
[stevieb]: ...unrelated which requires replacing a lot of code and a whole lib. I'm about to go nix only ffs
[shmem]: stevieb: what you're doing sounds afwully complex. Too much for me this evening to provide brighter insight ;-)
[stevieb]: I don't even own a Windows computer. Both my girl and I have a laptop each with Linux. I'm supporting Windows in some of my projects and I can't even guage whether it's worth it or not.
[stevieb]: shmem It's something I desired to have years ago, which is why I took over berrybrew. Cross-platform build/test automation locally, or over the network Test::BrewBuild
[shmem]: sounds good.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-03-28 22:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (342 votes). Check out past polls.