public
Created

  • Download Gist
code.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 
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,
output.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
{
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", ""],
],
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.