A little late, but wanted to show a slightly different way. Really, much of it follows liverpole's ideas but saves the data as it's string instead of an array of values.

Chris

#!/usr/bin/perl use strict; use warnings; use File::Basename; use Sort::Naturally; my %data; my $table; my %col_cnt; @ARGV = glob "tab*.txt"; while (<>) { next if /^ID/; chomp; ($table = fileparse $ARGV) =~ s/\.txt$// unless $table; $col_cnt{$table} ||= tr/|//; my ($gene, $cols) = split /\s*\|\s*/, $_, 2; $data{$gene}{$table} = $cols; $table = '' if eof; } my @tables = nsort keys %col_cnt; for my $gene (nsort keys %data) { print $gene; for my $table (@tables) { if ($data{$gene}{$table}) { print ' | ' . $data{$gene}{$table}; } else { print ' | ' . join " | ", ("n.a.") x $col_cnt{$table}; } } print "\n"; }
Update: Changed from storing the output in a string and waiting until the the entire output was stored and then printed to printing as the program proceeds.

Update: Changed from built-in sort to 'nsort' (use Sort::Naturally). If the fields being sorted have part of their name a number exceeding 9,(gene1 gene2 gene9 gene10) or (tab1 tab2...tab11 tab12), they won't sort properly using the default sort.


In reply to Re: Merging/Rearranging Tables by Cristoforo
in thread Merging/Rearranging Tables by homeveg

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.