#! usr/bin/perl #This script takes multiple files that contain the same number of rows, #and builds a matrix where the number of columns would equal the number of files. use warnings; use strict; #opens all txt files in the current directory #and stores the names of the files in @files array opendir(FILES,".")||die "Cannot open files in the directory\n"; my @files=(); my @matrix=(); for(readdir(FILES)){ if($_=~/\.txt/){ push(@files, $_); } } my $n=scalar(@files); print "This directory contains $n \.txt files\n"; #opens a file which will contain all the ratios open(TREE,">matrix.txt"); #goes through each file in @files array for my $i(0..$#files){ print "Working on \.\.\. $files[$i]\.\n"; open(FH,"<$files[$i]"); my $j=0; while(my $line=){ chomp $line; my @line=split("\t",$line); #This is more applicable if the file contained more than one column, tab delimited #@matrix is an array of array #jth array in @matrix contains records from different files for jth line push @{$matrix[$j]}, "$line[0]"; $j++; } } for my $tmp(@matrix){ #$records joins the elements by tab my $records=join "\t", @$tmp; print TREE "$records\n"; } exit;