in reply to Data::Dumper print arrays of scalars on one line

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 :)

Replies are listed 'Best First'.
Re^2: Data::Dumper print arrays of scalars on one line
by Danny (Chaplain) on Jun 23, 2024 at 02:20 UTC
    Interesting. I didn't realize dump/pp would put different hash keys etc on the same long line because it my "real" case it printed like below. I guess it's because all of the contents of the 'cell' hash key didn't fit within the value of $Data::Dump::LINEWIDTH I used which was 640,000. However, I'm not sure why it decided not to put some of the other hash keys/values on the same line. I just tried it with 1,000,000 and it did print the whole structure on a single line like in your example. If you set $Data::Dump::LINEWIDTH = 10 in your example, it prints like your "new dump string". It looks like you can just play around with the LINEWIDTH until you get what you want.

    EDIT: Regarding the comment "However, I'm not sure why it decided not to put some of the other hash keys/values on the same line.", it seems that if a particular item at a given level is deserving of its own line then all items at that level are.

    { active => 0, attr => [], cell => [ [], [undef, "Peptide Groups P> [undef, "Checked", "FALSE> [undef, "Confidence", "Hi> [undef, "Annotated Sequen> [undef, "Modifications", > [undef, "Qvality PEP", 6.> [undef, "Qvality q-value"> [undef, "# Protein Groups> [undef, "# Proteins", 1, > [undef, "# PSMs", 3, 6, 1> [undef, "Master Protein A> [undef, "Positions in Mas> [undef, "Modifications in> [undef, "# Missed Cleavag> [undef, "Theo. MH+ [Da]",> [undef, "Abundance: F667:> [undef, "Abundance: F668:> [undef, "Abundance: F669:> [undef, "Abundance: F670:> [undef, "Quan Info", unde> [undef, "Found in Sample:> [undef, "Found in Sample:> [undef, "Found in Sample:> [undef, "Found in Sample:> [undef, "Confidence (by S> [undef, "Percolator q-Val> [undef, "Percolator PEP (> [undef, "XCorr (by Search> [undef, "Top Apex RT [min> ], hidden => 0, label => "combined", maxcol => 29, maxrow => 3941, merged => [], mincol => 1, minrow => 1, parser => 0, }