Question Ephraim Malane · Oct 19, 2022

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

line = file.ReadLine()

description = $P(line,",","9")
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,,,,,,,,,,

Product version: IRIS 2020.2

Comments

Julius Kavay · Oct 19, 2022

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 traps 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=34s f='f continues 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)............:

0
Vitaliy Serdtsev · Oct 20, 2022

Here are two ways:

<FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"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,,,,,,,,,,G"
</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%DeepSee.TermList</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%ParseCSVRecord</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">,.</FONT><FONT COLOR="#800000">arr1</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">zw </FONT><FONT COLOR="#800000">arr1

</FONT><FONT COLOR="#0000ff">w </FONT><FONT COLOR="#000000">!

</FONT><FONT COLOR="#0000ff">s </FONT><FONT COLOR="#800000">list</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$$</FONT><FONT COLOR="#ff0000">CSVtoList</FONT><FONT COLOR="#000000">^%occLibrary(</FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">)

</FONT><FONT COLOR="#0000ff">d </FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%ListOfDataTypes</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">BuildValueArray</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">list</FONT><FONT COLOR="#000000">,.</FONT><FONT COLOR="#800000">arr2</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#0000ff">zw </FONT><FONT COLOR="#800000">arr2</FONT>


Take a look at the class methods %SQL.Util.Procedures

0