Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How can I read a file that is comma delimited but includes a comma in the string?

by Anonymous Monk
on May 27, 2003 at 23:37 UTC ( #261136=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

R,2164,27-2164,270102,Add Terminal Server to John, Jane, and Georges

The file has 5 strings seperated by commas:
R
2164
27-2164
270102
Add Terminal Server to John, Jane, and Georges

I'm trying to read Add Terminal Server to John, Jane, and Georges as one not:
Add Terminal Server to John
Jane
and Georges

Can you tell me how can I read this as one string and not three?

Comment on How can I read a file that is comma delimited but includes a comma in the string?
Re: How can I read a file that is comma delimited but includes a comma in the string?
by Ovid (Cardinal) on May 27, 2003 at 23:48 UTC

    If another program is writing that file out, then you need to go to the author of that program and tell them to either learn how to create proper CSV files or pick a format that they can actually use. That line would typically have the last field quoted.

    If it's always the case that the last field might have commas and the previous fields never have commas, you could do a restricted split:

    my @fields = split ',', $data, 5;

    I wouldn't do that, though, because you don't know what's going to change in the future. Whatever program you have creating the CSV data is writing bad data and you shouldn't have to put up with that.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      if all fields were in double quotes then how could I include it and not print the quotes?

        The Perl module Text::CSV can be really helpful here, since it's designed to handle things like quotes for you.

        If not P, what? Q maybe?
        "Sidney Morgenbesser"

        Directly from the POD of Text::CSV_XS:

        require Text::CSV_XS; my $csv = Text::CSV_XS->new; my $column = ''; my $sample_input_string = '"I said, ""Hi!""",Yes,"",2.34,,"1.09" +'; if ($csv->parse($sample_input_string)) { my @field = $csv->fields; my $count = 0; for $column (@field) { print ++$count, " => ", $column, "\n"; } print "\n"; } else { my $err = $csv->error_input; print "parse() failed on argument: ", $err, "\n"; } my @sample_input_fields = ('You said, "Hello!"', 5.67, 'Surely', '', '3.14159'); if ($csv->combine(@sample_input_fields)) { my $string = $csv->string; print $string, "\n"; } else { my $err = $csv->error_input; print "combine() failed on argument: ", $err, "\n"; }

        Cheers,
        Ovid

        New address of my CGI Course.
        Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: How can I read a file that is comma delimited but includes a comma in the string?
by TomDLux (Vicar) on May 27, 2003 at 23:59 UTC

    If the field containing internal commas is quoted:

    http://search.cpan.org

    In the search box, type CSV.

    If the internal commas cannot be distinguished from commas which separate fields, explain to the person who wrote the code feeding you that output that WalMart has a special on machine guns this week.

Re: How can I read a file that is comma delimited but includes a comma in the string?
by Skeeve (Vicar) on May 28, 2003 at 06:54 UTC
    If these conditions are met:
    1. There are always 5 Fields
    2. Additional commas only appear in the fifth field
then this code will give you the desired result:
while ($line=<DATA>) { chomp $line; ($char, $num1, $num2, $num3, $commafield)=split(/,/,$line,5); print <<MYDATA; $char $num1 $num2 $num3 $commafield ------- MYDATA } __DATA__ R,2164,27-2164,270102,Add Terminal Server to John, Jane, and Georges $,1111,22-2222,333333,Ignore mails from support, microsoft or bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://261136]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2014-09-19 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls