ewgf2002
asked on
Joining rows in a csv file using Perl
Below is an example of a exported data file from an application that I am working with. I would like skip the first row and then join the rows so that I can load them into an Oracle external table.
Exported file:
(spsku),item,location
30759@ROCK,30759,ROCK
,indmd,Case
30759@RK07,30759,RK07
,indmd,Case
,,MIN_TIME,MAX_TIME,0.0000 00
750@GTWN,750,GTWN
,indmd,Case
468@GTWN,468,GTWN
,indmd,Case
779@GTWN,779,GTWN
,indmd,Case
780@GTWN,780,GTWN
,indmd,Case
85338@GTWN,85338,GTWN
,indmd,Case
,,10/ 2/2006 0:00:00,10/ 3/2006 0:00:00,3564.000000
,,10/ 3/2006 0:00:00,10/ 4/2006 0:00:00,4316.000000
,,10/ 4/2006 0:00:00,10/ 5/2006 0:00:00,8128.000000
,,10/ 5/2006 0:00:00,10/ 6/2006 0:00:00,8382.000000
,,10/ 6/2006 0:00:00,10/ 7/2006 0:00:00,5974.000000
Desired format:
30759@ROCK,30759,ROCK,indm d,Case
30759@RK07,30759,RK07,indm d,Case,,MI N_TIME,MAX _TIME,0.00 0000
750@GTWN,750,GTWN,indmd,Ca se
468@GTWN,468,GTWN,indmd,Ca se
779@GTWN,779,GTWN,indmd,Ca se
780@GTWN,780,GTWN,indmd,Ca se
85338@GTWN,85338,GTWN,indm d,Case,,10 / 2/2006 0:00:00,10/ 3/2006 0:00:00,3564.000000,,10/ 3/2006 0:00:00,10/ 4/2006 0:00:00,4316.000000,,10/ 4/2006 0:00:00,10/ 5/2006 0:00:00,8128.000000,,10/ 5/2006 0:00:00,10/ 6/2006 0:00:00,8382.000000,,10/ 6/2006 0:00:00,10/ 7/2006 0:00:00,5974.000000
Exported file:
(spsku),item,location
30759@ROCK,30759,ROCK
,indmd,Case
30759@RK07,30759,RK07
,indmd,Case
,,MIN_TIME,MAX_TIME,0.0000
750@GTWN,750,GTWN
,indmd,Case
468@GTWN,468,GTWN
,indmd,Case
779@GTWN,779,GTWN
,indmd,Case
780@GTWN,780,GTWN
,indmd,Case
85338@GTWN,85338,GTWN
,indmd,Case
,,10/ 2/2006 0:00:00,10/ 3/2006 0:00:00,3564.000000
,,10/ 3/2006 0:00:00,10/ 4/2006 0:00:00,4316.000000
,,10/ 4/2006 0:00:00,10/ 5/2006 0:00:00,8128.000000
,,10/ 5/2006 0:00:00,10/ 6/2006 0:00:00,8382.000000
,,10/ 6/2006 0:00:00,10/ 7/2006 0:00:00,5974.000000
Desired format:
30759@ROCK,30759,ROCK,indm
30759@RK07,30759,RK07,indm
750@GTWN,750,GTWN,indmd,Ca
468@GTWN,468,GTWN,indmd,Ca
779@GTWN,779,GTWN,indmd,Ca
780@GTWN,780,GTWN,indmd,Ca
85338@GTWN,85338,GTWN,indm
ASKER
Here is what I have and I am not getting any results. I pretty much new to perl.
########################## ########## ########## ########## ########## ########
#
# Function: process_records
#
# Description: This function adds the records to an array.
#
# Returns: @RecordList
#
########################## ########## ########## ########## ########## ########
sub process_records
{
if ($ProdCount == 0)
{
while ($CurrentValue)
{
next unless $ProdCount++;
print "\n" if $ProductCount>3 && !/^,/;
chomp;
$RecordList[$ProdCount][0] = $Header;
$RecordList[$ProdCount][1] = $Item;
$RecordList[$ProdCount][2] = $Location;
$RecordList[$ProdCount][3] = $IndDmd;
$RecordList[$ProdCount][4] = $Uom;
$RecordList[$ProdCount][5] = $Garbage;
$RecordList[$ProdCount][6] = $StartDate;
$RecordList[$ProdCount][7] = $EndDate;
#$CurrentValue = <INPUTFILE>;
} # End while
} # End if
return @RecordList;
} # End sub process_records
########################## ########## ########## ########## ########## ########
#
# Function: print_list
#
# Description: This sub routine prints the list of row values in the
# proper format:
# ProductCode~Location~Date~ Qty
#
# Returns: @RecordList
#
########################## ########## ########## ########## ########## ########
sub print_list
{
# Print the records to the first 'wpl' file
for $ProcessRow (1..$#RecordList)
{
print OUTPUTFILE "$RecordList[$ProcessRow][ 0]~$Record List[$Proc essRow][1] ~$RecordLi st[$Proces sRow][2]~$ RecordList [$ProcessR ow][3]~$Re cordList[$ ProcessRow ][4]~$Reco rdList[$Pr ocessRow][ 5]~$Record List[$Proc essRow][6] ~$RecordLi st[$Proces sRow][7]\n ";
} # End for
close (OUTPUTFILE2);
return @RecordList;
} # End sub print_list
##########################
#
# Function: process_records
#
# Description: This function adds the records to an array.
#
# Returns: @RecordList
#
##########################
sub process_records
{
if ($ProdCount == 0)
{
while ($CurrentValue)
{
next unless $ProdCount++;
print "\n" if $ProductCount>3 && !/^,/;
chomp;
$RecordList[$ProdCount][0]
$RecordList[$ProdCount][1]
$RecordList[$ProdCount][2]
$RecordList[$ProdCount][3]
$RecordList[$ProdCount][4]
$RecordList[$ProdCount][5]
$RecordList[$ProdCount][6]
$RecordList[$ProdCount][7]
#$CurrentValue = <INPUTFILE>;
} # End while
} # End if
return @RecordList;
} # End sub process_records
##########################
#
# Function: print_list
#
# Description: This sub routine prints the list of row values in the
# proper format:
# ProductCode~Location~Date~
#
# Returns: @RecordList
#
##########################
sub print_list
{
# Print the records to the first 'wpl' file
for $ProcessRow (1..$#RecordList)
{
print OUTPUTFILE "$RecordList[$ProcessRow][
} # End for
close (OUTPUTFILE2);
return @RecordList;
} # End sub print_list
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
while( <DATA> ){
next unless $n++;
print "\n" if $n>3 && !/^,/;
chomp;
print;
}
print "\n";
__DATA__
(spsku),item,location
30759@ROCK,30759,ROCK
,indmd,Case
30759@RK07,30759,RK07
,indmd,Case
,,MIN_TIME,MAX_TIME,0.0000
750@GTWN,750,GTWN
,indmd,Case
468@GTWN,468,GTWN
,indmd,Case
779@GTWN,779,GTWN
,indmd,Case
780@GTWN,780,GTWN
,indmd,Case
85338@GTWN,85338,GTWN
,indmd,Case
,,10/ 2/2006 0:00:00,10/ 3/2006 0:00:00,3564.000000
,,10/ 3/2006 0:00:00,10/ 4/2006 0:00:00,4316.000000
,,10/ 4/2006 0:00:00,10/ 5/2006 0:00:00,8128.000000
,,10/ 5/2006 0:00:00,10/ 6/2006 0:00:00,8382.000000
,,10/ 6/2006 0:00:00,10/ 7/2006 0:00:00,5974.000000