Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here is my version, reads from __DATA__ if there is no 'Data.txt'
#!/usr/bin/perl --- use strict; use warnings; use autodie; Main(@ARGV); exit(0); sub Main { my $input = shift @_ || 'Data.txt'; my $output = shift @_ || 'Output.txt'; eval { open my ($infh), '<', $input; open my ($outfh), '>', $output; ReadThenPrint( $infh, $outfh ); 1; } or ReadThenPrint( \*DATA, \*STDOUT ); ## demo } ## end sub Main sub ReadThenPrint { my ( $infh, $outfh ) = @_; my @rows; my $colid = 0; while (<$infh>) { next if /^\s+$/; chomp; if (/^#id(\d+)/) { $colid = $1 - 1; next; } my ( $rowid, $num ) = split /\s+/, $_, 2; push @{ $rows[$rowid][$colid] }, $num; } ## end while (<$infh>) #~ use DDS; #~ Dump( \@rows ); print join( "\t", "#", map { 1 + $_ } 0 .. $colid ), "\n"; for my $rowid ( 0 .. $#rows ) { my @cols = @{ $rows[$rowid] }; use List::Util qw' max '; my $max = max map { $#$_ } @cols; for my $maxid ( 0 .. $max ) { print "$rowid\t"; for my $col (@cols) { print $col->[$maxid] if $col and ref $col and defined $col->[$ +maxid]; print "\t"; } print "\n"; } ## end for my $maxid ( 0 .. $max) } ## end for my $rowid ( 0 .. $#rows) } ## end sub ReadThenPrint __DATA__ #id1 1 90 2 80 3 70 #id2 1 70 2 40 2 40 3 20 4 5 #id3 0 0 0 0 0 0
output seems to be what you want (also included debugging DDS Dump )
$ARRAY1 = [ [ ( undef ) x 2, [ ( 0 ) x 3 ] ], [ [ 90 ], [ 70 ] ], [ [ 80 ], [ ( 40 ) x 2 ] ], [ [ 70 ], [ 20 ] ], [ undef, [ 5 ] ] ]; # 1 2 3 0 0 0 0 0 0 1 90 70 2 80 40 2 40 3 70 20 4 5

In reply to Re^3: transforming XY data to X and Multiple Y column data? by Anonymous Monk
in thread transforming XY data to X and Multiple Y column data? by ihperlbeg

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found