A
anirban
dear all
i am a beginner in perl.i am familiar with regex.but somewhere in the
net i hv found the following example which i cdnt understand.and there
was no explanation given in it.it would be very much helpful for me if
anyone kindly explain the following snippet of code.i understood what
it is actually doing.its jst retrieving the comma separated values frm
$text one by one.but i cdnt understand the regex part inside the while
loop inside the parse_csv subroutine.
$text = q<XYZZY,"","O'Reilly, Inc","Wall, Larry","a \"glug\"
bit,",5,"Error, Core Dumped">;
@new=&parse_csv($text);
foreach(@new){
print "$_\n";
}
sub parse_csv {
my $text = shift; # record containing comma-separated values
my @new = ();
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)",?
| ([^,]+),?
| ,
}gx;
push(@new, undef) if substr($text, -1,1) eq ',';
return @new; # list of values that were comma-separated
}
thanking you in advance
anirban
i am a beginner in perl.i am familiar with regex.but somewhere in the
net i hv found the following example which i cdnt understand.and there
was no explanation given in it.it would be very much helpful for me if
anyone kindly explain the following snippet of code.i understood what
it is actually doing.its jst retrieving the comma separated values frm
$text one by one.but i cdnt understand the regex part inside the while
loop inside the parse_csv subroutine.
$text = q<XYZZY,"","O'Reilly, Inc","Wall, Larry","a \"glug\"
bit,",5,"Error, Core Dumped">;
@new=&parse_csv($text);
foreach(@new){
print "$_\n";
}
sub parse_csv {
my $text = shift; # record containing comma-separated values
my @new = ();
push(@new, $+) while $text =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)",?
| ([^,]+),?
| ,
}gx;
push(@new, undef) if substr($text, -1,1) eq ',';
return @new; # list of values that were comma-separated
}
thanking you in advance
anirban