text: nitro322 0 Posted September 6, 2005 Share Posted September 6, 2005 I'm writing a function to parse a line from a CSV file. The particular CSV files I'm working with are delimited by commas, but add quote around fields that contain a ,. So, for example, it might contain the following line:Test String1,var2,"Company, Inc.", Test String3As you can see, something like a simple stringsplit() will not work here. Since I was unable to find any other UDFs specifically for handling this, I decided to write my own. I figured it'd be best to handle it as a recursive function, and I got the logic down to parse each field (the msgbox() line in the following code displays the correct strings when uncommented, in reverse order), but I can't figure out how to properly return it in an array. Basically, I can't figure how to pass the array and write to it during each recursive call, and then return the final product to the function call.Below is a copy of my function, as well as some example code to test it. Any ideas how I can get the recursion handled properly? Thanks.$line = 'Test String1,var2,"Company, Inc.", Test String3' $splitline = _CSVSplit($line, 0) for $i = 0 to ubound($splitline) - 1 msgbox(0,"$splitline[" & $i & "]", $splitline[$i]) next ; function to split quoted CSV lines func _CSVSplit($inline, $dim) dim $linearr[99] if stringleft($inline, 1) == '"' then $end = stringinstr($inline, '",', 0, 1) if not $end then $end = stringlen($inline) $linearr[$dim] = stringmid($inline, 2, $end - 1) else $linearr[$dim] = stringmid($inline, 2, $end - 2) $linearr[$dim+1] = _CSVSplit(stringtrimleft($inline, $end+1), $dim+1) endif else $end = stringinstr($inline, ',', 0, 1) if not $end then $end = stringlen($inline) $linearr[$dim] = stringleft($inline, $end) else $linearr[$dim] = stringleft($inline, $end - 1) $linearr[$dim+1] = _CSVSplit(stringtrimleft($inline, $end), $dim+1) endif endif msgbox(0,'test',$linearr[$dim]) return $linearr