perlquestion
TStanley
Greetings fellow Monks!
<p>
I have been tasked with creating a CSV file that will be imported into a new document archiving system,
and after a lot of hacking, I have turned a 100MB text file into a file just under 1MB in size after
removing the non-needed parts and getting it formatted. Below is a sample of what I am currently working with:
</p>
<CODE>
1401,PERISH STORE INV,Quarterly,(V) INV810 USER NAME (EDIT REPORT IF 05309 PRSH INV),EXTRACT WAS RUN),(V) INV820 USER NAME (PERISHABLE INVENTORY REPORT),(VTD8) D:\DEPT\ACCT\MAIL\INV820.DAT,(V) INV820C USER NAME (PERISHABLE INVENTORY-BAKERY),(QUARTERLY RUN ONLY)
1402,PERSH INV BOOKS,Quarterly,(V) INV805 58 COPIES - USER NAME,(V) INV805 2 COPIES - USER NAME,(V) INV805A XTRA COPIES-SAVE IN COMPUTER ROOM,ANNUAL STORE INVENTORY ONLY:,(V) INV805 58 COPIES - USER NAME,(V) INV805 2 COPIES - USER NAME,(V) INV805A XTRA COPIES-SAVE IN COMPUTER ROOM
1403,BAKERY INV BOOKS,Quarterly,(V) INV805 35 COPIES - USER NAME,(V) INV805A 5 COPIES - SAVE IN COMPUTER ROOM,ANNUAL STORE INVENTORY ONLY:,(J) INV805 35 COPIES - USER NAME,(J) INV805A 5 COPIES - SAVE IN COMPUTER ROOM
1501,INV PRICE GUIDE,As Needed,(V) MSC052 BOTH COPIES ARE TO BE PUT IN BINDERS AND,AND LEFT ON THE CABINETS BEHIND THE DP,SECRETARY'S DESK
</CODE>
<p>
What I need to accomplish from the above is to break down the fourth field and beyond so that each one is
on its own line, along with the first three lines, so the first line would look something like:
</p>
<CODE>
1401,PERISH STORE INV,Quarterly,(V) INV810 USER NAME (EDIT REPORT IF 05309 PRSH INV)
1401,PERISH STORE INV,Quarterly,(EXTRACT WAS RUN)
1401,PERISH STORE INV,Quarterly,(V) INV820 USER NAME (PERISHABLE INVENTORY REPORT)
1401,PERISH STORE INV,Quarterly,(VTD8) D:\DEPT\ACCT\MAIL\INV820.DAT
1401,PERISH STORE INV,Quarterly,(V) INV820C USER NAME (PERISHABLE INVENTORY-BAKERY)(QUARTERLY RUN ONLY)
</CODE>
<p>
As you can see, what makes this a challenge is that each line is a different size depending upon how many reports
there are to be distributed, as well as the number of people that the reports go to.
</p>
<p>
My plan is to take each line of the csv file and split it into an array with the 4th element being a reference
to another array which whould contain all of the remaining lines. How would I go about creating the output csv
file to look like my desired output? My code so far is below:
</p>
<CODE>
#!C:\Perl\bin\perl
use strict;
use warnings;
open INPUT "input.csv"||die "Can not open: $!\n";
open OUTPUT "output.csv"||die "Can not open: $!\n";
while(<INPUT>){
my @line = split /,/;
my @line2 = @line[0..2];
my @dist = @line[3..$#line];
my $ref = \@dist;
push @line2,$ref;
## Print to OUTPUT
}
close INPUT;
close OUTPUT;
</CODE>
<div class="pmsig"><div class="pmsig-21156">
<BR>TStanley<BR>
--------<BR>
<FONT SIZE=-1><I>People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell</I></FONT><BR>
</div></div>