Skip to main content

I want to fileload a CSV/XLXS  file and process the contents, but I don't know how to identify the carriage return line feed in order to be able to break the contents of the file into 'rows' I can then process - maybe with $ replace - or is there an easier way of reading in a CSV to process the rows in it?

Thanks!



------------------------------
Toni Davenport
Uniface Community Edition Shared Account
US
------------------------------

I want to fileload a CSV/XLXS  file and process the contents, but I don't know how to identify the carriage return line feed in order to be able to break the contents of the file into 'rows' I can then process - maybe with $ replace - or is there an easier way of reading in a CSV to process the rows in it?

Thanks!



------------------------------
Toni Davenport
Uniface Community Edition Shared Account
US
------------------------------

$replace(string,1,"%%^",".;",-1)

regards 

Iain



------------------------------
Iain Sharp
Head of Technical Services
Pci Systems Ltd
Sheffield GB
------------------------------

$replace(string,1,"%%^",".;",-1)

regards 

Iain



------------------------------
Iain Sharp
Head of Technical Services
Pci Systems Ltd
Sheffield GB
------------------------------

To expand on this, 

You can use the replace above to turn a CSV into a list, where each line is an entry in the list

Then, the code below will turn the csv file into a struct. for parsing, but similar code could (for example) return a list of columns instead. 

v_string = $replace(v_string,1,"%%^","+.",-1)
v_struct = $newstruct
forlist v_line, v_counter in v_string
   v_struct->line{v_counter} = $newstruct
   call lp_dec_csv_line(v_line, v_struct->line{v_counter})
endfor
..........


entry lp_dec_csv_line
params
	string p_line : INOUT
	struct p_item : INOUT
endparams
variables
	string v_line, v_item
	numeric v_pos, v_pos2, v_counter
	boolean v_inquotes
endvariables

p_item = $newstruct
v_line = p_line
p_line = ""
while(v_line != "")
	v_counter += 1
	v_item = ""
	if(v_line[1,1] = '"')
		v_line = v_line[2]
		v_inquotes = 1
		while(v_inquotes)
			v_pos = $scan(v_line,'"')
			if(v_line[v_pos+1,1] = '"'  ) ; Double quotes 
				v_item = $concat(v_item, v_line[1,v_pos])
				v_line = v_line[v_pos+2]
			else
				v_item = $concat(v_item, v_line[1,v_pos-1])
				v_line = v_line[v_pos+2]
				v_inquotes = 0
			endif
		endwhile
	else
		v_pos = $scan(v_line,',')
		if(v_pos = 0)
			v_item = v_line
			v_pos = $length(v_line)
		elseif(v_pos > 1)
			v_item = v_line[1,v_pos-1]
		else
			v_item = ""
		endif
		v_line = v_line[v_pos+1]
	endif
	putitem p_line, v_counter, v_item
	p_item->"Col%%v_counter%%%" = v_item
endwhile
return (0)
end


------------------------------
Iain Sharp
Head of Technical Services
Pci Systems Ltd
Sheffield GB
------------------------------

To expand on this, 

You can use the replace above to turn a CSV into a list, where each line is an entry in the list

Then, the code below will turn the csv file into a struct. for parsing, but similar code could (for example) return a list of columns instead. 

v_string = $replace(v_string,1,"%%^","+.",-1)
v_struct = $newstruct
forlist v_line, v_counter in v_string
   v_struct->line{v_counter} = $newstruct
   call lp_dec_csv_line(v_line, v_struct->line{v_counter})
endfor
..........


entry lp_dec_csv_line
params
	string p_line : INOUT
	struct p_item : INOUT
endparams
variables
	string v_line, v_item
	numeric v_pos, v_pos2, v_counter
	boolean v_inquotes
endvariables

p_item = $newstruct
v_line = p_line
p_line = ""
while(v_line != "")
	v_counter += 1
	v_item = ""
	if(v_line[1,1] = '"')
		v_line = v_line[2]
		v_inquotes = 1
		while(v_inquotes)
			v_pos = $scan(v_line,'"')
			if(v_line[v_pos+1,1] = '"'  ) ; Double quotes 
				v_item = $concat(v_item, v_line[1,v_pos])
				v_line = v_line[v_pos+2]
			else
				v_item = $concat(v_item, v_line[1,v_pos-1])
				v_line = v_line[v_pos+2]
				v_inquotes = 0
			endif
		endwhile
	else
		v_pos = $scan(v_line,',')
		if(v_pos = 0)
			v_item = v_line
			v_pos = $length(v_line)
		elseif(v_pos > 1)
			v_item = v_line[1,v_pos-1]
		else
			v_item = ""
		endif
		v_line = v_line[v_pos+1]
	endif
	putitem p_line, v_counter, v_item
	p_item->"Col%%v_counter%%%" = v_item
endwhile
return (0)
end


------------------------------
Iain Sharp
Head of Technical Services
Pci Systems Ltd
Sheffield GB
------------------------------

Thanks so much for this, Iain, is all good! 



------------------------------
Toni Davenport
Uniface Community Edition Shared Account
US
------------------------------