Skip to content

Instantly share code, notes, and snippets.

@kentfredric
Created November 1, 2011 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentfredric/1329456 to your computer and use it in GitHub Desktop.
Save kentfredric/1329456 to your computer and use it in GitHub Desktop.
use strict;
sub splitCommaNotQuote {
my ( $line ) = @_;
my @fields = ();
while ( $line =~ m/((\")([^\"]*)\"|[^,]*)(,|$)/g ) {
if ( $2 ) {
push( @fields, $3 );
} else {
push( @fields, $1 );
}
last if ( ! $4 );
}
return( @fields );
}
use Data::Dump qw( pp );
my ( @lines ) = <DATA>;
my $content = join q{}, @lines;
chomp for @lines;
pp { content => [ splitCommaNotQuote( $content ) ] };
pp { lines => [ map { [ splitCommaNotQuote( $_ )] } @lines ] };
__DATA__
COLLOQ_TYPE,COLLOQ_NAME,COLLOQ_CODE,XDATA
S,"BELT,FAN",003541547,
S,"BELT V,FAN",000324244,
S,SHROUD SPRING SCREW,000868265,
S,"D" REL VALVE ASSY,000771881,
S,"YBELT,"V"",000323030,
S,"YBELT,'V'",000322933,
S,"YBELT HAS A LINE
FEED IN IT",00000001,
{
content => [
"COLLOQ_TYPE",
"COLLOQ_NAME",
"COLLOQ_CODE",
"XDATA\nS",
"BELT,FAN",
"003541547",
"\nS",
"BELT V,FAN",
"000324244",
"\nS",
"SHROUD SPRING SCREW",
"000868265",
"\nS",
"\"D\" REL VALVE ASSY",
"000771881",
"\nS",
"\"YBELT",
"\"V\"\"",
"000323030",
"\nS",
"YBELT,'V'",
"000322933",
"\nS",
"YBELT HAS A LINE\nFEED IN IT",
"00000001",
"\n",
],
}
{
lines => [
["COLLOQ_TYPE", "COLLOQ_NAME", "COLLOQ_CODE", "XDATA"],
["S", "BELT,FAN", "003541547", ""],
["S", "BELT V,FAN", "000324244", ""],
["S", "SHROUD SPRING SCREW", "000868265", ""],
["S", "\"D\" REL VALVE ASSY", "000771881", ""],
["S", "\"YBELT", "\"V\"\"", "000323030", ""],
["S", "YBELT,'V'", "000322933", ""],
["S", "\"YBELT HAS A LINE"],
["FEED IN IT\"", "00000001", ""],
],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment