Uniface User Forum

 View Only
  • 1.  Fileload a CSV, how do I now process the lines/identify the CRLF?

    Posted 03-11-2023 14:08

    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
    ------------------------------


  • 2.  RE: Fileload a CSV, how do I now process the lines/identify the CRLF?

    Posted 03-12-2023 16:13

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

    regards 

    Iain



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



  • 3.  RE: Fileload a CSV, how do I now process the lines/identify the CRLF?

    Posted 03-13-2023 08:22

    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
    ------------------------------



  • 4.  RE: Fileload a CSV, how do I now process the lines/identify the CRLF?

    Posted 03-14-2023 09:24

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



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