Bobby wrote:
I have a text file with five colums of data (ColumA-E). Within each
column there could be multiple data with a comma seperating each piece of
data. How do i white a loop to parse through the data set and assign
each column a variable and each piece of data within that column a
different variable?
I haven't done much coding on this yet but here's the idea:
test.txt:
ColumA|ColumB|ColumC|ColumD|ColumE
Test|A,F|ACY,FAC|ACYA,FCSA,FCSC|ACYEA,FCSA2
Test2|A|A1,A2,A3,A4,A5,A6|B1,B2|C1,C2,C3
Test3|B|b10,B11,B12,B13,|C1,C2|
#!/usr/bin/perl
use strict;
use warnings;
my $File = 'test.txt';
my $output = 'ouput.txt';
open FILE, '<', $File or die "Could not open
open OUT, '>', $output or die "Could not open '$output' $!";
'$File' $!";
while ( <File> ) {
next if $. == 1; # exclude header
chomp;
my ( $ColA, $ColB, $ColC, $ColD,$ColE ) = split /\|/;
#Note: should i assign the different data elements inside of each
# $ColA, $ColB,etc. into an array here?
#For example, I want to split ColC of the first row in test.txt
#into different variables so that i can assign different variables names
#to each of the data within that colum...i.e. $ColD_1=ACYA,
#$ColD_2=FCSA...etc. What i want to do is print each variable out into a
text
#file (output.txt)
};
}
close FILE;
print OUT;
__END__
Thanks for any suggestions.
I assume that you want something like this:
my @data;
<FILE>; # exclude header
while ( <FILE> ) {
chomp;
push @data, [ map /,/ ? [ split /,/ ] : $_, split /\|/, $_, -1 ];
}
If, however, you want everything in an Array of Arrays then:
my @data;
<FILE>; # exclude header
while ( <FILE> ) {
chomp;
push @data, [ map [ split /,/ ], split /\|/, $_, -1 ];
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/