Text::Table will align your output. Here is a sample program. Also, I used Sort::Naturally so that names with trailing digits will sort correctly, i.e. when they are greater than 1 digit long.
Update: in while loop, changed from split on space to split on tabs because thats how the fields are separated.
#!/usr/bin/perl
use strict;
use warnings;
use Text::Table;
use Sort::Naturally;
my %data;
my @col2 = qw/ PA TT AT /;
while (<DATA>) {
chomp;
my ($name, $col2, $col3) = split /\t/;
push @{ $data{$name}{$col2} }, $col3;
}
my $tb = Text::Table->new( map {title => $_}, @col2);
for my $name (nsort keys %data) {
my @tmp;
local $" = ',';
for my $col2 (@col2) {
push @tmp, $data{$name}{$col2} ?
"@{ $data{$name}{$col2} }" :
"";
}
$tb->load(\@tmp);
}
print $tb;
__DATA__
Name_1 TT XL_927799.1
Name_1 PA PA_392
Name_1 AT ZX_003039195.1
Name_2 TT XL_931313.1
Name_2 AT ZX_003043016.1
Name_3 TT XL_929616.1
Name_3 PA PA_5040
Name_3 PA PA_6336
Name_4 TT XL_928294.1
Name_4 PA PA_917
This prints:
PA TT AT
PA_392 XL_927799.1 ZX_003039195.1
XL_931313.1 ZX_003043016.1
PA_5040,PA_6336 XL_929616.1
PA_917 XL_928294.1
-
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.
|