#id1 #id2 #id3 0 0 1 90 1 70 2 80 2 40 3 70 3 20 4 5 #### #id1 #id2 #id3 0 0 1 90 70 2 80 40 3 70 20 4 5 #### #!/usr/bin/perl use strict; use Data::Dumper 'Dumper'; my $input = shift @ARGV || 'Data.txt'; my $output = shift @ARGV || 'Output.txt'; print $input, "\n"; open(DATA, $input) || die "cannot open $input for reading"; open(OUT, ">", $output) || die "cannot open $output for writing"; my $genome; my %phage=(); my @pg=(); my @id=(); while () { chomp; if ( /#/ ) { $genome = $line; push( @pg, $genome ); } else { my ( $id, $abd ) = split( ' ', $line ); push(@id, $id); $phage{$id}{$genome}=$abd; } } my %hash = map { $_ => 1 } @id; my @sorted_id = sort { ( $a <=> $b) } keys %hash; print OUT join( "\t", '', sort @pg ), "\n"; for my $sorted_id ( @sorted_id ) { print OUT $sorted_id, "\t"; for my $pg ( @pg ) { print OUT "$phage{$sorted_id}{$pg}\t"; } print OUT "\n"; }