PERL : multidimentional array

Platform window xp, perl 8.5
-------------------------------
Input file has following data:

ABC|asd|asadd|aadd|addff|.............|zxv|
ABC|asd|asadd|aadd|addff|.............|zxv|
ABC|asd|asadd|aadd|addff|.............|zxv|

Here '.........' indicates x number of elements, in between.
Output should either in in tabular form or should look like

ABC     asd     asadd     aadd     addff     .............     zxv     
ABC     asd     asadd     aadd     addff     .............     zxv
ABC     asd     asadd     aadd     addff     .............     zxv  
#!/usr/bin/perl
$data_file="Input.txt";
open(DATFILE, $data_file) || die("Could not open file!");
@array=<DATFILE>;
close(DATFILE);

foreach $element (@array)
{
chomp($element);
($a,$b,$c)=split(/\|/,$element); 
print "$a\t$b\t\t$c\t\t\n";
print $_;
}   

Above captures 3rows and 3 columns.
Output looks like

ABC     asd     asadd   
ABC     asd     asadd   
ABC     asd     asadd

My questions are :
a)How to accommodate n numbers columns, any generic way of including all columns in any row?
b)my $rows= @array; #this will gives total number of rows printed.How to capture total number of elements in a row?

a) put the splitted stuff in another temporary array, e.g. @rowarr
b) my $noofelements=@rowarr;

Try to replace this:

($a,$b,$c)=split(/\|/,$element); 
print "$a\t$b\t\t$c\t\t\n";
print $_;

with

my @rowarr=split(/\|/,$element);

 foreach my $record (@rowarr) {
 chomp($record);
 print "$record\t";            }
print "\n";
1 Like

Why split around like mad? Assuming your original file is in test.psv

$ perl -p -l -i.bak -e 'print STDERR "File $ARGV, Line $., substituted ",s/\|/\t/g, "occurences"' test.psv
File test.psv, Line 1, substituted 7occurences
File test.psv, Line 2, substituted 7occurences
File test.psv, Line 3, substituted 7occurences
$ cat test.psv
ABC     asd     asadd   aadd    addff   .............   zxv
ABC     asd     asadd   aadd    addff   .............   zxv
ABC     asd     asadd   aadd    addff   .............   zxv

pseudocoder , It worked!

Is there any way we can represent this data in tabular format too. e.g.
-----------------------
| abc | def |
------------------------
| ijk | lmn |
------------------------

Yes.

$ cat input.data
ABC|asd|asadd|aadd|addff|dfgfdg|zxv|
ABC|asd|asadd|aadd|addff|dsfgfd|zxv|
ABC|asd|asadd|aadd|addff|weerwe|zxv|
$ ./readinputdata.pl
---------------------------------------------------
| ABC | asd | asadd | aadd | addff | dfgfdg | zxv | 
---------------------------------------------------
| ABC | asd | asadd | aadd | addff | dsfgfd | zxv | 
---------------------------------------------------
| ABC | asd | asadd | aadd | addff | weerwe | zxv | 
---------------------------------------------------
$ 
cat readinputdata.pl
#!/usr/bin/perl

my $data_file='input.data';

open(DATFILE, $data_file) || die("Could not open file!");
@array=<DATFILE>;
close(DATFILE);

foreach $element (@array)
{
chomp($element);
my @rowarr=split(/\|/,$element);
print "-"x51,"\n";
print "\| ";

 foreach my $record (@rowarr) {
 chomp($record);
 print "$record \| ";           }
print "\n";
}
print "-"x51,"\n";

Note: In this case the output looks nice, but only because the records have identical length and the number of "-" sign is "hardcoded".
I guess the output won't be pretty with your actual data.
Maybe I can - a bit later - code something for the case where the record length will vary...

Yes the actual data is of variable size.

---------- Post updated at 01:08 PM ---------- Previous update was at 12:31 PM ----------

what is use Text::Table ?

This one is meant for experimental use and it will handle varying field widths. There are at least two cases which I noticed while testing it, where it will misbehave, namely if one of the fields is empty and if the number of fields per line varies. But if you can decipher my code, you can easily fix it.

$ cat input.data
ABCDE|asd|aserreadd|aadd|addff|werredfgfdg|zxv|
ABC|asafdd|asadd|aadd|addff|dsfgfd|zxewrwev|
ABCDEFG|asd|asadd|wwereweaadd|addff|weerwe|zxrev|
$ perl readinputdata.pl
-------------------------------------------------------------------------------
| ABCDE   | asd    | aserreadd | aadd        | addff | werredfgfdg | zxv      |
-------------------------------------------------------------------------------
| ABC     | asafdd | asadd     | aadd        | addff | dsfgfd      | zxewrwev |
-------------------------------------------------------------------------------
| ABCDEFG | asd    | asadd     | wwereweaadd | addff | weerwe      | zxrev    |
-------------------------------------------------------------------------------
$ 
#!/usr/bin/perl

my @temparr=();
my @rowsize=();
my @maxsize=();

my $data_file='input.data';

open(DATFILE, $data_file) || die("Could not open file!");
@array=<DATFILE>;
close(DATFILE);

############################## create @temparr with field length values

foreach $element (@array)
{
chomp($element);
my @rowarr=split(/\|/,$element);

 foreach my $record (@rowarr) {
 chomp($record);

 my $rowsize=@rowarr;
 push @rowsize, $rowsize;

 my $size=length($record);
 push @temparr, "$size ";
 }
 push @temparr, "\n";
}

########################################## put widest fields in @maxarr

my $counter=0;
my $maxcounter=0;

foreach my $record (@temparr) {
chomp($record);

   if ($counter <= $rowsize[0])
   {
   push @maxarr, $record;
   $counter++ ;
   }
   elsif
   (($maxcounter < $rowsize[0]) && ($record > $maxarr[$maxcounter]))
   {
   $maxarr[$maxcounter]=$record;
   $maxcounter++;
   }
   elsif
   ($maxcounter < $rowsize[0])
   {
   $maxcounter++;
   }
   else
   {
   $maxcounter=0;
   }

}

########################################################### print table

$topbottom += $maxarr[$_] for 0 .. $#maxarr;
$topbottom = $topbottom + $rowsize[0] + $rowsize[0]*2 + 1;

my $counter=0;
my $currelement=0;

foreach $element (@array)
{
chomp($element);

print "-"x$topbottom,"\n";
print "\|";

my @rowarr=split(/\|/,$element);

 foreach my $record (@rowarr) {
 chomp($record);

 if ($counter < $rowsize[0]) {
 $maxarr[$currelement] =~ s/\s//g;
 my $format="%-" . $maxarr[$currelement] . "s";
 printf(" $format \|", "$record");
 $counter++;
 $currelement++;
 }
}
print "\n";
$counter=0;
$currelement=0;
}
print "-"x$topbottom,"\n";

Text::Table is a Perl module, probably meant for cases like this, but I don't have any experience with it.

Is there a way to control the font type and size, when you print in the output file?