Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All

I have a big problem, I am parsing a huge csv file and using split to get the data but the problem is that one of the columns is a 'TEXT' column and the entries have commas in, e.g.

Name,Email,Text
Fred,demo@demo,Hi my name is Fred, I like shopping.

my ($name, $email, $text) = split(/\,/,$_);

Is there anyway to ignore all commas after the last one needed? This is causing me a real headache... any comments much appreciated.. the file is huge so I cant go through and replace the commas in the last field.

Replies are listed 'Best First'.
Re: File Parsing REGEX
by diotalevi (Canon) on Feb 11, 2004 at 00:34 UTC
Re: File Parsing REGEX
by Roger (Parson) on Feb 11, 2004 at 00:23 UTC
    Use the split /PATTERN/,EXPR,LIMIT form of split, specify a limit, which is the maximum number of fields split will split the record into.

    my ($name, $email, $text) = split /,/, $_, 3;

      For case

      my ($name, $email, $text) = split /,/, $_, 3;
      it's working but how about case
      my ($name, $email, $text, $status) = split /,/, $_;
      ???

      ~ Schiller

Re: File Parsing REGEX
by nite_man (Deacon) on Feb 11, 2004 at 13:26 UTC

    Try to look at Text::CSV.

    Also, if all fields within CSV are surrounded by double-quotes, you can use this pattern:

    open CVS, '/tmp/file.cvs' or die "Cannot open CVS file: $!"; while(my $row = <FILE>) { my ($name, $email, $text) =~ m!\,?\"(.*?)\"\,?!g; # do something } close CVS;

    ~ Schiller