Remove text within quotes in a string
I am reading file values by position with comma-separated string and it gives me incorrect values on the below line because there is a comma within double quotes within a string.
I would like to remove any text that has quotes with object script or alternatively separate each value with pipe-delimited so that I can return position 8 as code and position 9 as a description
s description = $P(line,",","9")
s code = $P(line,",","8")
12162,CHAPTER I,Certain infectious and parasitic diseases (A00-B99),003 (A20-A28),Certain zoonotic bacterial diseases,A28,"Other zoonotic bacterial diseases, not elsewhere classified",A28,"Other zoonotic bacterial diseases, not elsewhere classified",N,N,N,N,N,,,,,,,,,,
Assuming, fields which contains commas are quoted ("aaa,bbb,ccc") and (for the simplicity) fields does not contains quotes, then something like this should do the job
ClassMethod CSV(filename) { s old=$system.Process.SetZEOF(1) // use $zeof instead of error trap s result=[] o filename:"r":0 i $t { u filename while '$zeof { read line i line]"" do result.%Push(..fields(line)) // ignore empty lines } } c filename d $system.Process.SetZEOF(old) q result } ClassMethod fields(line) { s a="", f=0, row=[] f i=1:1:$l(line) { s c=$a(line,i) i c=44,'f d row.%Push(a) s a="" continue i c=34 s f='f continue s a=a_$c(c) } q row }
A test output:
USER>s res=##class(DC.Help).CSV(fn) USER>zso res (0).(0).............: 12162 (0).(1).............: CHAPTER I (0).(2).............: Certain infectious and parasitic diseases (A00-B99) (0).(3).............: 003 (A20-A28) (0).(4).............: Certain zoonotic bacterial diseases (0).(5).............: A28 (0).(6).............: Other zoonotic bacterial diseases, not elsewhere classified (0).(7).............: A28 (0).(8).............: Other zoonotic bacterial diseases, not elsewhere classified (0).(9).............: N (0).(10)............: N (0).(11)............: N (0).(12)............: N (0).(13)............: N (0).(14)............: (0).(15)............: (0).(16)............: (0).(17)............: (0).(18)............: (0).(19)............: (0).(20)............: (0).(21)............: (0).(22)............:
Here are two ways:
Take a look at the class methods %SQL.Util.Procedures