global function integer gf_parse_plus (string as_string, ref string
as_array[], ref integer ai_count, string as_delimiter);// Function Takes a
string and separates elements into an array
// Returns 0 = success
// 1 = error
//
// as_string Source string containing values delimited by as_delimiter
// as_array[] Destination array, passed by value
// ai_count Returns count of items in array
// as_delimiter delimiter value
int li_pos // position of commas
int li_len // string parsing
int li_start // string parsing
string ls_item // single item parsed from string
if Len ( as_string ) < 1 then
ai_count = 0
return 1
end if
// parse out items and insert into array
li_pos = 1
li_start = 1
ai_count = 0
DO WHILE li_pos > 0
li_pos = Pos ( as_string, as_delimiter, li_start )
if li_pos = 0 then
ls_item = Mid ( as_string, li_start )
else
ls_item = Mid ( as_string, li_start, li_pos - li_start )
end if
ls_item = Trim ( ls_item )
if Len ( ls_item ) > 0 then
ai_count++
as_array[ai_count] = ls_item
end if
li_start = li_pos + 1
LOOP
return 0
--
Terry Dykstra (TeamSybase)
http://powerbuilder.codeXchange.sybase.com/
http://casexpress.sybase.com/cx/cx.stm
product enhancement requests:
http://my.isug.com/cgi-bin/1/c/submit_enhancement
<Kemal> wrote in message news:4803885f.644...@sybase.com...
I find most of the time you don't really need an array since you intend
to process the data from start to finish anyway. I often found myself
writing code like the following to parse strings;
do while str<>''
value=left(str,pos(str+';',';')-1)
str=mid(str,pos(str+';',';')+1)
// process value....
loop
So at some point I generalised that coding pattern and wrote a helper
function;
global function boolean f_token (ref string as_source, string as_delim,
ref string as_value);
boolean lb_ret
long ll_pos
lb_ret=as_source<>''
ll_pos=Pos (as_source + as_delim, as_delim)
as_value = trim (Left (as_source, ll_pos - 1))
as_source = trim (Mid (as_source, ll_pos + Len (as_delim)))
return lb_ret
end function
Then I overloaded it with different data types to convert the value
automatically;
global function boolean f_token (ref string as_source, string as_delim,
ref decimal adc_value)
global function boolean f_token (ref string as_source, string as_delim,
ref datetime adtm_value)
global function boolean f_token (ref string as_source, string as_delim,
ref boolean ab_value)
...
(conversion and error handling left as an exercise for the reader)
So now I end up with code that looks like this;
ls_file = filereadex( ....
do while f_token(ls_file, '~r~n', ls_line)
f_token(ls_line, ',', ldc_key)
f_token(ls_line, ',', ls_name)
...
insert ...
values (:ldc_key, :ls_name, ...
loop