Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parse .csv

Status
Not open for further replies.

EchoCola

Programmer
Apr 13, 2004
48
US
Hey guys i have this subrouting to parse a comma delimited file. I'm just confused on the pattern, could someone explain this part?
Code:
"([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,
       }gx;

Code:
sub parse_csv {
    my $text = shift;      # record containing comma-separated values
    my @new  = ();
    push(@new, $+) while $text =~ m{
        # the first part groups the phrase inside the quotes.        
        "([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,
       }gx;
       push(@new, undef) if substr($text, -1,1) eq ',';
       return @new;      # list of values that were comma-separated
 
Way too hard EchoCola - be lazy and use the Text::CSV module from CPAN, you can find it at:
Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Hi EchoCola

To be honest I have used this routine many times, and it is working very good! But I have never tried to understand how it does the csv.

Cheers!

PD:You could start reading the book " Mastering regular Expressions " from O'reilly. If you really need to know how to use it. It's not a simpl regex.
 
Yeah, I know its from that o'reily book i just don't have the actual book. I think I will go look at CPAN and see what that module has to offer.

Thanks for your replies
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top