<?xml version="1.0" encoding="windows-1252"?>
<node id="924308" title="Re: reformatting tab delimited file" created="2011-09-05 19:24:52" updated="2011-09-05 19:24:52">
<type id="11">
note</type>
<author id="421781">
Cristoforo</author>
<data>
<field name="doctext">
[mod://Text::Table] will align your output. Here is a sample program. Also, I used [mod://Sort::Naturally] so that names with trailing digits will sort correctly, i.e. when they are greater than 1 digit long.&lt;p&gt;
Update: in while loop, changed from split on space to split on tabs because thats how the fields are separated.&lt;p&gt;

&lt;c&gt;#!/usr/bin/perl
use strict;
use warnings;
use Text::Table;
use Sort::Naturally;

my %data;
my @col2 = qw/ PA TT AT /;

while (&lt;DATA&gt;) {
	chomp;
	my ($name, $col2, $col3) = split /\t/;
	push @{ $data{$name}{$col2} }, $col3;
}

my $tb = Text::Table-&gt;new( map {title =&gt; $_}, @col2);

for my $name (nsort keys %data) {
	my @tmp;
	local $" = ',';
	for my $col2 (@col2) {
		push @tmp, $data{$name}{$col2} ?
			"@{ $data{$name}{$col2} }" :
			"";
	}
	$tb-&gt;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
&lt;/c&gt;
&lt;p&gt;This prints:&lt;p&gt;
&lt;c&gt;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&lt;/c&gt;</field>
<field name="root_node">
924257</field>
<field name="parent_node">
924257</field>
</data>
</node>
