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



  • 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.