NOTSomebody has asked for the wisdom of the Perl Monks concerning the following question:
$csv = "test.csv"; open(DAT, $csv) || die("Cannot Open File"); while (<DAT>) { my @new = (); push(@new, $+) while $_ =~ m{ "([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),? | , }gx; push(@new, undef) if substr($_, -1,1) eq ','; my $string = format_string(@new); my $query = qq{Insert into person_info VALUES ($string)}; print $query, "\n"; } sub format_string { my $string; foreach (@_) { if (/[^\d]/) { $string .= qq{"$_",}; } else { $string .= qq{$_,}; } } return substr($string, 0, -1); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Help with CSV file parsing
by ikegami (Patriarch) on Aug 05, 2008 at 07:46 UTC | |
Re: Help with CSV file parsing
by chrism01 (Friar) on Aug 05, 2008 at 07:46 UTC | |
by NOTSomebody (Initiate) on Aug 05, 2008 at 08:15 UTC | |
Re: Help with CSV file parsing
by davorg (Chancellor) on Aug 05, 2008 at 09:03 UTC | |
Re: Help with CSV file parsing
by starX (Chaplain) on Aug 05, 2008 at 08:32 UTC | |
Re: Help with CSV file parsing
by Bloodnok (Vicar) on Aug 05, 2008 at 10:05 UTC |