in reply to Merging/Rearranging Tables
Chris
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.#!/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 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merging/Rearranging Tables
by homeveg (Acolyte) on Feb 13, 2007 at 14:37 UTC | |
by Cristoforo (Curate) on Feb 13, 2007 at 17:15 UTC | |
by homeveg (Acolyte) on Feb 16, 2007 at 14:25 UTC | |
by Cristoforo (Curate) on Feb 17, 2007 at 21:17 UTC |