Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
File Splitting

New User

Jan 30, 2003, 8:10 AM

Views: 2571
File Splitting

I am having a csv file with 5 columns.One of the column is
a description of length 50characters and may have any characters like comma or hypen as part of the description. I am using comma as the delimiter to split the file to do the validations as the columns may not be of fixed length. But if the description has a comma ,
it is being split in to two
sample data looks like this...

C5,L10,ACTB,"OldACTB-MultisDecile1,2""", gokavars
C5,L05,ACTM,New LTV/ACTB - Manhattan , gokavars
N4,L52,ACTB,ACTB - 3X+ MULTIS DECILE 4 ,gokavars

After running my program
my data looks like this

C5 | L10 |ACTB |"OldACTB-MultisDecile1| 2"""
C5 | L05 | ACTM | New LTV/ACTB - Manhattan | gokavars
N4 |L52 |ACTB | ACTB - 3X+ MULTIS DECILE 4 |gokavars

Is there anyway I can make the first row look like
C5 | L10 |ACTB |"OldACTB-MultisDecile1 2""" | gokavars


Jan 30, 2003, 9:47 AM

Views: 2567
Re: [gokavar] File Splitting

Your best bet would be to pick a delimiter that is unlikely to show up in any of your data, such as a pipe | or a carat ^. Perhaps even use a double character for a delimiter || or ^^.

"...there's some good in this world, Mr. Frodo, and it's worth fighting for." - Samwise Gamgee, The Two Towers

New User

Jan 30, 2003, 11:41 AM

Views: 2561
Re: [Melvosh] File Splitting

This is a specific requirement for me where I have to use a comma delimited file. I figured out something called
quotewords function,but it is returning some error..

here is the piece of the code..

open(f1, "<$infile") || die "Can't open $infile $!" ; #--f1 is input data file

local @newarr=<f1>;

here am getting an error as

" Unmatched quote"..

Any idea


Feb 3, 2003, 1:54 AM

Views: 2547
Re: [gokavar] File Splitting

The Text::ParseWords module (that comes with the standard Perl distribution) can be used to process CSV files with embedded commas.

If your requirements get any more complex then you should look at the Text::CSV module from CPAN.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks