PERL : multidimentional array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL : multidimentional array
# 1  
Old 05-18-2010
PERL : multidimentional array

Platform window xp, perl 8.5
-------------------------------
Input file has following data:
Code:
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

Code:
ABC     asd     asadd     aadd     addff     .............     zxv     
ABC     asd     asadd     aadd     addff     .............     zxv
ABC     asd     asadd     aadd     addff     .............     zxv


Code:
#!/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
Code:
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?

Last edited by pludi; 05-18-2010 at 08:07 AM.. Reason: code tags, please...
# 2  
Old 05-18-2010
a) put the splitted stuff in another temporary array, e.g. @rowarr
b) my $noofelements=@rowarr;

Try to replace this:
Code:
($a,$b,$c)=split(/\|/,$element); 
print "$a\t$b\t\t$c\t\t\n";
print $_;

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

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

This User Gave Thanks to pseudocoder For This Post:
# 3  
Old 05-18-2010
Why split around like mad? Assuming your original file is in test.psv
Code:
$ 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

# 4  
Old 05-18-2010
pseudocoder , It worked!

Is there any way we can represent this data in tabular format too. e.g.
-----------------------
| abc | def |
------------------------
| ijk | lmn |
------------------------
# 5  
Old 05-18-2010
Yes.
Code:
$ 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 | 
---------------------------------------------------
$

Code:
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...
# 6  
Old 05-18-2010
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 ?
# 7  
Old 05-18-2010
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.

Code:
$ 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    |
-------------------------------------------------------------------------------
$

Code:
#!/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";

Quote:
Originally Posted by shristi
what is use Text::Table ?
Text::Table is a Perl module, probably meant for cases like this, but I don't have any experience with it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multidimentional arrays in KSH

Hi, I am using KSH shell and need to define a multi dimensional array in which i need to store and retrive data. Please provide your valuable in puts. Eg: asbjkasd 1234 asdhnas 1254 i need to store the above values and use them as input to another command. Note each line is a pair. Thanks... (8 Replies)
Discussion started by: hraj1984
8 Replies

2. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

5. Shell Programming and Scripting

perl array help !

I have a list of items in array say @array1=(apple=2,orange=3,peas=15,spinach=50); Another array has values say @array2=(apple,orange,plums,pineapple); I need to display a list if @array2 values is present in @array1 .else I need to print of the form(The final o/p may look like this.) OUTPUT... (5 Replies)
Discussion started by: gameboy87
5 Replies

6. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

how to get last value in an array in perl

Hi, I have a set of file names say: file1 file2 file3 .. filen I may just do a ls -l or however it works to read this list into an array... I need to pick the last file name that is "filen" from the list... your help is greatly appreciated.....hope im clear on my question... (7 Replies)
Discussion started by: meghana
7 Replies
Login or Register to Ask a Question