Re: Exploding column-data question
Posted in 1994
CAVILA@zycon.com writes:
>Hello,
> I have a varchar column with data that looks like this example:
> my_list A/BC/D/EFG/H/
> The "/" character can be replaced by any printable character, it is only
> there to serve as a delimiter.
> I'd like to assign this to a record variable and then use a display array
> to have my form end up looking like this:
> [ A]
> [ BC]
> [ D]
> [EFG]
> [ H]
> My question is: can this be done easily using Informix 4GL? Can someone
> please post a way to parse through the list and address each of the data
> elements? Also, would there be a way to address one specific piece of
> data, without having to look at each character? (ie: my_list(3) = "D")
Refer to a single character via subscript (ie: my_list[3] = "D"), you got it
right except that you need to use brackets. You can refer also to a subscripted
range (my_list[2,4] = "YES"; characters in positions 2 through 4 inclusive).
So, here's the array loading routine:
FUNCTION load_array(string)
DEFINE string CHAR(###)
DEFINE la_array ARRAY[##] OF RECORD
val CHAR(3)
END RECORD
DEFINE l,x,y,arr SMALLINT
LET l = LENGTH(string)
IF l > 0 THEN
LET arr = 0
LET x = 1
FOR y = 1 TO l
IF string[y] = "/" THEN
# delimiter. Fill in the array and increment
LET arr = arr + 1
IF arr > ## THEN
ERROR "More than ", ##, " array rows required"
EXIT FOR
END IF
IF y = 1 OR x = y THEN
# the first character in the string was a delimiter,
# or we has two delimiters in a row, set to NULL
LET la_array[arr].val = NULL
ELSE
# set the value to the x through y-1 charactes
IF y - x > 3 THEN
# more than 3 characters between delimiters
ERROR "WARNING! array value ", arr USING "<<<&",
" was truncated."
LET la_array[arr].val = string[x,y-1]
END IF # increment the next starting point
LET x = y + 1
CONTINUE FOR
END IF
END FOR
IF string[l] != "/" AND arr < ## THEN
# get the last one
LET arr = arr + 1
# set the value to the x through l characters
LET la_array[arr].val = string[x,l]
END IF
END IF
CALL set_count(arr)
DISPLAY ARRAY ...
============================================================
Dennis J. Pimple Informix Software Inc
Senior Consultant Denver Colorado USA
dennisp@informix.com Voice:303-850-0210 Fax:303-779-4025