Pulling Characters from a string
Pulling Characters from a string
(OP)
I need helping pulling characters from a prompt.
The prompt:
Enter Product Class Code: (#, #;#, [RETURN]=ALL, E=Exit)
Then Entries:
1. The Operator can enter EN (Product Class).
or
2. The Operator can enter EN;TJ;KI (Multiple Product Classes).
I'm having problem with #2.
I want to pull the Product Classes only.
The number of product classes can change, depending on the entry.
I would like to loop through that string using the ";" as a count to find out how many entries there are. I will then use those entries for a sql.
Please, the easiest and fastest code would be appricated.
Ken S.
The prompt:
Enter Product Class Code: (#, #;#, [RETURN]=ALL, E=Exit)
Then Entries:
1. The Operator can enter EN (Product Class).
or
2. The Operator can enter EN;TJ;KI (Multiple Product Classes).
I'm having problem with #2.
I want to pull the Product Classes only.
The number of product classes can change, depending on the entry.
I would like to loop through that string using the ";" as a count to find out how many entries there are. I will then use those entries for a sql.
Please, the easiest and fastest code would be appricated.
Ken S.
RE: Pulling Characters from a string
Some time ago, I had to parse a string based on a character delimiter. Appended is the function using your test code. 4GL doesn't do these functions real well since the strings passed are fixed length.
I have a number of string related 4GL functions. Should I add a FAQ to this forum?
Regards,
Ed
MAIN
DEFINE
tmp_string CHAR(20),
ret_string CHAR(10),
cnt SMALLINT
LET cnt = 1
LET tmp_string = "EN;TJ;KI"
# while loop breaks at the 4th loop
WHILE cnt > 0
CALL parse_str(";", cnt, tmp_string) RETURNING ret_string, cnt
END WHILE
END MAIN
# 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
LET cnt = i # return the positon ended
RETURN ret_string, cnt
END FUNCTION
RE: Pulling Characters from a string
Thanks for the response.
That would be a great idea. You guys know more about the tricks of informix than I do at this point in time.