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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |