Friday, June 26, 2009

4GL comma split function

not really.. but if you want to iterate over a delimited string via 4gl, you can do the following:

(parse_str function adapted from http://www.tek-tips.com/faqs.cfm?fid=2364 )


# This function searches string for an instance of char_delim
# starting at position cnt and returns the string ending at the next
# char_delim or the end of string
######################################################################
function parse_str(char_delim, cnt, string)
######################################################################
define
char_delim CHAR(1), # character delimiter
cnt SMALLINT, # postion in string to start counting
string CHAR(300), # string to search
ret_string CHAR(40), # string returned
i, # index
x, # counter
string_len SMALLINT # string length

LET string_len = LENGTH(string)

IF string_len = 0 # got a null string
THEN
RETURN "",0
END IF

IF string[cnt] = char_delim
THEN
LET cnt = cnt + 1 # get off the delimiter
END IF
IF cnt > string_len
THEN # return when the end of the string is reached
RETURN "",0
END IF

LET x = 1
FOR i = cnt TO string_len
IF string[i] = char_delim
THEN
EXIT FOR
END IF
LET ret_string[x] = string[i] # save a character
LET x = x + 1
END FOR

return ret_string, i # return the positon ended
end function

function split(f_orig)
define f_orig,f_str char(100), x, f_pos smallint
let x = length(f_orig)
let f_pos = 1
while (f_pos > 0 and f_pos < x)
call parse_str(",",f_pos,f_orig) returning f_str, f_pos
display f_str, f_pos
end while
end function

main
call split("some,comma,delimited,string")
end main

No comments:

Post a Comment