Sunday, January 25, 2015

Read and write matrix file Perl

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

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

open OUT," > matrixout.txt" or die "$!\n";
#Print output
for my $i (0..$#data)
{
for my $j (0..$#{$data[0]})
{
   print OUT "$data[$i][$j]","\t";
}
   print OUT "\n";
}

No comments: