thread277-1601818
Further to the previous thread, I needed to parse several .csv files with a variable number of columns, where the columns may be quoted with embedded commas and or imbedded quotes, typical of user files from Excel.
I ended up with this, I’m sure it could be improved but it does work!
It might help, written for Regina Rexx,
Phil
Further to the previous thread, I needed to parse several .csv files with a variable number of columns, where the columns may be quoted with embedded commas and or imbedded quotes, typical of user files from Excel.
I ended up with this, I’m sure it could be improved but it does work!
It might help, written for Regina Rexx,
Phil
Code:
/* ************** REXX ************** */
call rxfuncadd 'sysloadfuncs', 'rexxutil', 'sysloadfuncs'
call sysloadfuncs; rc=sysloadfuncs(); rcm=rxfuncerrmsg()
if rc<>0 then do rcc=RxMessageBox('Unable to load REXXUTIL.', 'Rexx Fatal Error:', 'Cancel'); exit 1; end
InFile='c:/DATA/odds.csv'
rc=RegStemRead(InFile,inrec)
if rc <> 0 then do rcc=RxMessageBox('Read Error on' InFile, 'IO Error:', 'Cancel'); exit 1; end
ocnt=0; orec.=''
do r = 1 to inrec.0
call CSVSplit(inrec.r)
if result=0 then do
say 'Record' r
do i=1 to CSVcol.0
say 'Col('||right(i,3,'0')||')='||CSVcol.i
end
end
end
exit 0
CSVSplit: Procedure Expose CSVcol.
parse arg CSVRec
inquotes=0 ; CSVcol.='' ;CSVcol.0=0 ; CSVfield=1
if CSVRec='' then do ; return 1 ; end
do CSVchr=1 to length(CSVRec)
if inquotes=0 then do
if substr(CSVRec,CSVchr,1)='"' then do
inquotes=1 ; iterate ; end
if substr(CSVRec,CSVchr,1)=',' then do
CSVfield=CSVfield+1 ; iterate ; end
CSVcol.CSVfield=CSVcol.CSVfield||substr(CSVRec,CSVchr,1)
iterate ; end
if substr(CSVRec,CSVchr,2)='""' then do
CSVcol.CSVfield=CSVcol.CSVfield||'"'
CSVchr=CSVchr+1 ; iterate ; end
if substr(CSVRec,CSVchr,2)='",' then do
inquotes=0 ; iterate ; end
CSVcol.CSVfield=CSVcol.CSVfield||substr(CSVRec,CSVchr,1)
end
CSVcol.0=CSVfield
return 0