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

Hello monks, When i print my hash like this, i've got a good alignment.,
$VAR1 = { 'a' => '1', 'b' => [ 1, 2 ] };
But when i would want to prefix something, then i am ending up in collapsing the alignment.,
My hash-> $VAR1 = { 'a' => '1', 'b' => [ 1, 2 ] };
I would want the 'a', and 'b' to be aligned appropriately with the first line. My code is like the following,
use Data::Dumper; $hash = { a => '1', b => [1, 2], }; print "\tMy hash-> " . Dumper $hash;
What should i do to make this printing properly ? What i have tried till now ? As i am not able to get a single idea about how to do this ?, Awaiting for precious response... Thanks.

Replies are listed 'Best First'.
Re: making dumper to align perfectly
by moritz (Cardinal) on Oct 14, 2009 at 12:58 UTC
    If by "align" you mean "insert a number of spaces (or a tab) at the start of each line", you're half-way there already
    my $d = Dumper $hash;; $d =~ s/^/\t/gm; print $d; # now indented
    Perl 6 - links to (nearly) everything that is Perl 6.
      There is an improvement in the alignment, but i dont know how to get as i expected. It gives this output,
      My hash-> $VAR1 = { 'a' => '1', 'b' => [ 1, 2 ] };
      But i would want, ( as said in the previous reply ).
        Then you need to insert more spaces. It's not hard to get from my suggestion to what you want. Show some effort.
        Perl 6 - links to (nearly) everything that is Perl 6.
        You just don't bother trying to adapt solutions given to you.
        my $d = Dumper $hash; my $somanyspaces = ' 'x10; # how long is your string? # add to all lines whitespaces $d =~ s/^/$somanyspaces/gm; # add to first line your string $d =~ s/^$somanyspaces/I am lazy /; print $d; # now indented
Re: making dumper to align perfectly
by toolic (Bishop) on Oct 14, 2009 at 12:50 UTC
      I want it like
      My hash-> $VAR1 = { 'a' => '1', 'b' => [ 1, 2 ] };
Re: making dumper to align perfectly
by leocharre (Priest) on Oct 14, 2009 at 14:05 UTC

    Maybe YAML would do some things you want. YAML is intended as serializing data for human beings to look at.

    Picking on the spacing of Dumper seems ... picky. Maybe a regex is what you need on that.

    I'd imagine Dumper is made for coders to look at data, not for humans.

Re: making dumper to align perfectly
by Anonymous Monk on Oct 14, 2009 at 15:33 UTC
    perhaps what you really want is
    print Data::Dumper->Dump([\%my_hash],[qw(*my_hash)]);
      What the OP really wants, IMO, is for us to provide a ready made solution.

      A user level that continues to overstate my experience :-))