Just for fun :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11160123 use warnings; use Data::Dump qw( pp ); my $VAR1 = { # wanted 'hk1' => 5, 'hk2' => [ [1, 2, 3], [4, 5, 8] ], }; #push @{$VAR1->{hk2}[0]}, "001" .. "010"; #$VAR1 = [ map [ $_ .. $_+29 ], 1 .. 11 ]; $Data::Dump::LINEWIDTH = 100; print "Original:\n\n"; my $orig = pp $VAR1; print "$orig\n"; print "\nThe new dump string:\n\n"; my $dump = arraysofscalarsonasingleline( $VAR1 ); print $dump; print "\n\nValidation of the new dump:\n\n"; my $test = pp eval $dump; print "$test\n"; print "\n", $orig eq $test ? "\e[92m VALID :)\e[m" : "\e[91m ERROR: Validation failed :(\e[m", "\n"; ###################################################################### +######### sub arraysofscalarsonasingleline { if( @_ > 1 ) { my @elements = map arraysofscalarsonasingleline($_), @_; return "(\n" . join(",\n", @elements) =~ s/^/ /gmr . "\n)"; } my $item = shift; if( 'HASH' eq ref $item ) { my @elements = map { my $key = arraysofscalarsonasingleline( $_ ); ref($item->{$_}) ? "$key =>\n" . arraysofscalarsonasingleline( $item->{$_} ) + =~ s/^/ /gmr : "$key => " . arraysofscalarsonasingleline( $item->{$_} ) } sort keys %$item; return "{\n" . join(",\n", @elements) =~ s/^/ /gmr . "\n}"; } elsif( 'ARRAY' eq ref $item ) { if( grep { ref } @$item and @$item > 1 ) { my @elements = join ",\n", map arraysofscalarsonasingleline( $_ +), @$item; return "[\n" . join(",\n", @elements) =~ s/^/ /gmr . "\n]"; } else { my @elements = join ', ', map arraysofscalarsonasingleline( $_ ) +, @$item; return "[ @elements ]"; } } else { use Data::Dump 'pp'; return pp $item; } }

Outputs:

Original: { hk1 => 5, hk2 => [[1, 2, 3], [4, 5, 8]] } The new dump string: { "hk1" => 5, "hk2" => [ [ 1, 2, 3 ], [ 4, 5, 8 ] ] } Validation of the new dump: { hk1 => 5, hk2 => [[1, 2, 3], [4, 5, 8]] } VALID :)

In reply to Re: Data::Dumper print arrays of scalars on one line by tybalt89
in thread Data::Dumper print arrays of scalars on one line by Danny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.