Sunday, January 25, 2015

Transpose Matrix Perl

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

my $file = 'matrixin.txt';
my @data;
open( FILE, $file ) or die "Can't open file '$file': $!";
while( ) {
chomp;
my @row = split;
push @data, \@row;
}
close( FILE );

#Print output
for my $j (0..$#{$data[0]})
{
for my $i (0..$#data)
{
print "$data[$i][$j]","\t";
}
print "\n";
}

No comments: