ikegami's solution is simple and to the point. It's really the most elegant solution I see, but it does rely on the fact that your input data has no values repeated among the table columns. In other words, foo1 has '1', and '1' appears nowhere else in the table. That may be a perfectly good assumption, and may work just fine for you, in which case, stop reading now. ;)

If the numeric values in the table are non-unique, the solution doesn't work, because the hash used to transform the table is keyed off of the table's numeric values. And as we know, hash keys are unique.

So, I came up with an alternate solution, which is far less elegant, but not constrained by unique tabular data. The numeric fields may be non-unique.

use strict; use warnings; use feature qw(say); my %foos; # Grab the table and put it into a hash of arrays. while( my $line = <DATA> ) { chomp $line; my( $foo, @values ) = split /\s+/, $line; $foos{$foo} = \@values; } # Hang onto the foos as keys in sorted order. my @foo_keys = sort keys %foos; # Figure out how wide the table is (must be uniform across foos). my $last_col = $#{ $foos{ $foo_keys[0] } }; foreach my $col ( 0 .. $last_col ) { print map { "$_\t$foos{$_}->[$col]\n" } @foo_keys; } __DATA__ foo1 1 4 7 foo2 2 5 8 foo3 3 6 9

My solution takes a shortcut which is also based on an assumption that may not be true. My solution relies on all rows having the same number of columns. If that were not the case, you would have to track how many columns each row has, and iterate accordingly. That adds a little complexity that I didn't think was needed. But I mention it because it would need to be dealt with if your rows were not of uniform length.


Dave


In reply to Re: Stack table columns with hash of arrays by davido
in thread Stack table columns with hash of arrays by robertkraus

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.