Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Data::Dumper print arrays of scalars on one line

by Danny (Friar)
on Jun 20, 2024 at 03:06 UTC ( [id://11160123]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a Data::Dumper option to print arrays of scalars on a single line, but everything else with indentation? For example, like:
$VAR1 = { 'hk1' => 5, 'hk2' => [ [1, 2, 3], [4, 5, 8] ], etc ...

Replies are listed 'Best First'.
Re: Data::Dumper print arrays of scalars on one line
by GrandFather (Saint) on Jun 20, 2024 at 03:49 UTC

    Not exactly what you want, but maybe Data::Dump is closer to your liking:

    use strict; use warnings; use Data::Dump qw(dump); my $data = { 'hk1' => 5, 'hk2' => [ [21], [100, 101, 102, 103, 104, 120, 23] ] }; print dump($data), "\n"; push @{$data->{hk2}[0]}, "01" .. "10"; print dump($data);

    Prints:

    { hk1 => 5, hk2 => [[21], [100 .. 104, 120, 23]] } { hk1 => 5, hk2 => [ [21, "01", "02", "03", "04", "05", "06", "07", "08", "09", +10], [100 .. 104, 120, 23], ], }
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      That worked well for my case with $Data::Dump::LINEWIDTH = $large_integer
Re: Data::Dumper print arrays of scalars on one line
by codiac (Beadle) on Jun 20, 2024 at 05:36 UTC
      This worked for my case with $Data::Dumper::Perltidy::ARGV = "-l=$large_integer" but it was very slow compared to Data::Dump.
      run time seconds ---------------- real user method 0.696 0.390 Data::Dump 44.42 33.45 Data::Dumper::Perltidy
Re: Data::Dumper print arrays of scalars on one line
by tybalt89 (Monsignor) on Jun 23, 2024 at 00:13 UTC

    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 :)
      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, }
Re: Data::Dumper print arrays of scalars on one line
by ikegami (Patriarch) on Jun 20, 2024 at 05:06 UTC

    No.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11160123]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-07-16 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.