Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to parse a file using comma delimiters???

Status
Not open for further replies.

bigpapi

Programmer
Aug 1, 2006
16
0
0
CA
I have a working procedure that uses file read to open a dat file and populate variables with the values from the dat file.

I had to change the dat file into a CSV file and i'm not sure how to do the populate on a comma delimited set of data.


Here is my old code:


ls_variable_1 = trim( mid( ls_file_lines[li_counter], 1,25 ) )
ll_variable_2 = long(trim( mid( ls_file_lines[li_counter], 26, 8 ) ))
ll_variable_3 = long(trim( mid( ls_file_lines[li_counter], 34, 8 ) ))


I need to know how do the above code only using a CSV file.


so:


ls_variable_1 = whatever comes before the first comma
ls_variable_2 = whatever comes after the first comma and before the second comma

and so on


Hope someone can help quick
thanks

 
Hi,

first you should know that this is a real NOT trivial problem!

But it is solvable in nearly all cases. %-)

you need to consider about these facts...

1. what kind of CSV-file do you have. Are text and numeric values distinguished by a " or ' ?

2. If text values are enclosed by " or ' is a value of the enclosing char marked special in a text(string)? like /" or ~"

3. How are numeric values written in the CVS? eg a value of 1.53 can be written as 1.53 or 1,53 which is depending on local settings in your machine... which does not have to be mandatory the same for all users!!!

I think there can be more than tese 3 pitfals using a CVS-File.

Now let us solve the proplem from the worts case to the easiest.

If all or some of the above ( or any other) problem can occur you need to code all the cases and handle it by your own. This means you need to code a parser like this (pseudocode)

Code:
FileRead( #file , ls_input) 
l_count = 1
l_len = len( the_string_fom file)
FOR l_pos = 1 TO l_len
    my_char = mid( ls_in, l_pos, 1)
    
    if reading_variable = no then
      reading_variable = numeric
      if my_char = " then reading_variable = string
      if my_char = , then reading_variable = no; var_no ++
    end if
    if reading_variable = numeric then
      line_var[var_no]+= my_char
      // and here you need to handle a possible colon as decimal separator
    end if
    if reading_variable = string then
      line_var[var_no]+= my_char
      // and here you need to handle a possible " as part of the stringn or set reading_variable = no if end of variable is reached ( closing " )
    end if
next

you see this can be very hard to be done and end in a long coding all cases of colons and points and decimals, quotes etc.

my be you'e in luck and the cvs is of good quality. this means:
all strings are quoted and no commy appear within a string.
decimals are written as 1.53 and NOT 1,53

you can use a function like this one to parese your imput_string

Code:
function string of_get_token (ref string as_string, string as_token);string	ls_ret = "", ls_string, ls_links
long		ll_token_laenge, ll_token_pos


ls_string 		= as_string + as_token
ll_token_laenge	= LEN( as_token )
ll_token_pos	= POS( ls_string , as_token )

IF ll_token_laenge > 0 THEN
	
	IF ll_token_pos > 0 THEN
		
		ls_ret = LEFT( ls_string , ll_token_pos -1 )
		
		ls_links = MID( as_string , ll_token_pos + ll_token_laenge)

		as_string = ls_links
	
	END IF

END IF


RETURN ls_ret


If you can manage it that the source file you want to read can be produced as a TAB-delimeted file instead of comma or semicolon-separated as CSV Than you can easily import the file into a datawindowand than parse your datawindow for getting the values.

hope you are not disapointed that there is no way for generic function for all possible filesformats and pitfals within CSV.
It Is really recomended - as far as I can see it - NOT to use CVS. It is much better to use TAB-deleimited files or Files with known fixed length parted rows as you had.

if you need to stay with CSV you should google with 'reading comma separated' or 'importing svc' and check if some forums or source-sites hava a code sample. this can be vc++ or basic or pascal or any. maybe there is a codesample handling the cases discussed on top

good luck
 
OK, i have something working now and everything is good except for my last field.

what is the option in powerbuilder 9 that will bring back the position of the end of a line
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top