Home > Archive > PERL Beginners > March 2008 > Loop within Loop
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
|
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,FC
SA2
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.
---------------------------------
Never miss a thing. Make Yahoo your homepage.
| |
| Gunnar Hjalmarsson 2008-03-18, 7:03 pm |
| 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?
It sounds as if you should create a data structure rather than multiple
scalar variables.
use Data::Dumper;
my @AoHoA;
<DATA>;
while (<DATA> ) {
chomp;
my (%cols, %tmp);
@cols{ qw/A B C D E/ } = split /\|/;
foreach ( qw/A B C D E/ ) {
push @{ $tmp{ "Column$_" } }, split /,/, $cols{$_};
}
push @AoHoA, \%tmp;
}
print Dumper \@AoHoA;
__DATA__
ColumA|ColumB|ColumC|ColumD|ColumE
Test|A,F|ACY,FAC|ACYA,FCSA,FCSC|ACYEA,FC
SA2
Test2|A|A1,A2,A3,A4,A5,A6|B1,B2|C1,C2,C3
Test3|B|b10,B11,B12,B13,|C1,C2|
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| John W. Krahn 2008-03-18, 7:03 pm |
| 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,FC
SA2
> 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
| |
| Hridyesh Pant 2008-03-18, 10:08 pm |
| you can use hash,where key is the column and value is the refrence to an
array,and this array can contain multiple data.
--Hridyesh
Gunnar Hjalmarsson wrote:
> Bobby wrote:
>
> It sounds as if you should create a data structure rather than
> multiple scalar variables.
>
> use Data::Dumper;
> my @AoHoA;
> <DATA>;
> while (<DATA> ) {
> chomp;
> my (%cols, %tmp);
> @cols{ qw/A B C D E/ } = split /\|/;
> foreach ( qw/A B C D E/ ) {
> push @{ $tmp{ "Column$_" } }, split /,/, $cols{$_};
> }
> push @AoHoA, \%tmp;
> }
> print Dumper \@AoHoA;
>
> __DATA__
> ColumA|ColumB|ColumC|ColumD|ColumE
> Test|A,F|ACY,FAC|ACYA,FCSA,FCSC|ACYEA,FC
SA2
> Test2|A|A1,A2,A3,A4,A5,A6|B1,B2|C1,C2,C3
> Test3|B|b10,B11,B12,B13,|C1,C2|
>
|
|
|
|
|