Wednesday, January 19, 2011

Multidimensional array

use strict;                             #unconstrct varible to findout purpose
use warnings;                           #warnings purpose
use File::Slurp;                        #to get file scalar, array or hash

my @ifile = read_file("E:/Purand/test/mtab.txt");

my $row = @ifile;                       #number of rows count
my $col = my @cols = (split /\t/, $ifile[0]);
                                        #number of columns count
my @array = ();                         #array initials
my @sfile = ();                         
my $i = 0;                              #index variable initials

foreach my $row (@ifile) {              #to get each and every line(row)
  @sfile = split(/\t/, $row);            #split every line and store array  
=begin
  foreach my $element (@sfile) {        #to get each element(cell)
     push @{$array[$i]}, $element;       #assign array of array(multiarray)
 }
=cut

$array[$i]=[@sfile];

  my @sfile = ();                       #reset array
  $i++;                                 #array(row) index increment
}

for(my $m = 0; $m < $row; $m++) {       #loop to be run no. of rows
   for(my $n = 0; $n < $col; $n++) {    #loop to run no. of cols
      print $array[$m]->[$n], "\t";     #print out each and every cell with alignment
   }
   print "\n";                          #every row seperation
}

No comments: