0

I wanted to know the Informix 4gl command to split a variable such as

lv_var = variable01;variable02

into

lv_var01 = variable01
lv_var02 = variable02

Is there something in Informix 4gl that can do this.

In python I could do

lv_array = lv_var.split(";")

and use the variables from the array

TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106

3 Answers3

1

It's possible with classic Informix 4gl with something like this...

define
    p_list      dynamic array of char(10)

main
    define
        i       smallint,
        cnt     smallint,
        p_str   char(500)

    let p_str = "a;b;c;d"
    let cnt = toarray(p_str, ";")

    for i = 1 to cnt
        display p_list[i]
    end for

end main

function toarray(p_str, p_sep)
    define
        p_str   char(2000),
        p_sep   char(1),
        i       smallint,
        last    smallint,
        ix      smallint,
        p_len   smallint

    let ix = 0
    let p_len = length(p_str)

    # -- get size of array needed
    for i = 1 to p_len
        if p_str[i] = p_sep then
            let ix = ix + 1
        end if
    end for

    if ix > 0 then
        # -- we have more then one
        allocate array p_list[ix + 1]

        let ix = 1
        let last = 1
        for i = 1 to p_len
            if p_str[i] = p_sep then
                let p_list[ix] = p_str[last,i-1]
                let ix = ix + 1
                let last = i + 1
            end if
        end for
        # -- set the last one
        let p_list[ix] = p_str[last, p_len]
    else
        # -- only has one
        allocate array p_list[1]
        let ix = 1
        let p_list[ix] = p_str
    end if

    return ix

end function

Out:

a
b
c
d

Dynamic array support requires IBM Informix 4GL 7.32.UC1 or higher

0

There isn't a standard function to do that. One major problem is returning the array. I'd probably write a C function to do the job, but in I4GL, it would look like:

FUNCTION nth_split_field(str, c, n)
    DEFINE str VARCHAR(255)
    DEFINE c   CHAR(1)
    DEFINE n   INTEGER

    ...code to find nth field delimited by c in str...

END FUNCTION
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

What you'll find is that the products that have grown to superceed Informix 4GL over the years such as FourJs Genero will have built-in methods that have been added to simplify the Informix 4GL developers life.

So something like this would do what you are looking for if you upgraded to Genero

-- Example showing how string can be parsed using string tokenizer

-- New features added to Genero since Informix 4gl used include
-- STRING - like a CHAR but length does not need to be specified -  http://www.4js.com/online_documentation/fjs-fgl-manual-html/?path=fjs-fgl-manual#c_fgl_datatypes_STRING.html
-- DYNAMIC ARRAY like an ARRAY but does not need to have length specified.  Is also passed by reference to functions - http://www.4js.com/online_documentation/fjs-fgl-manual-html/?path=fjs-fgl-manual#c_fgl_Arrays_010.html
-- base.StringTokenizer - methods to split a string - http://www.4js.com/online_documentation/fjs-fgl-manual-html/?path=fjs-fgl-manual#c_fgl_ClassStringTokenizer.html

MAIN

DEFINE arr DYNAMIC ARRAY OF STRING
DEFINE i INTEGER

    CALL string2array("abc;def;ghi",arr,";")

    -- display result
    FOR i = 1 TO arr.getLength()
        DISPLAY arr[i]
    END FOR

    -- Should display
    --abc
    --def
    --ghi

END MAIN


FUNCTION string2array(s,a,delimiter)
DEFINE s STRING 
DEFINE a DYNAMIC ARRAY OF STRING
DEFINE delimiter STRING

DEFINE tok base.StringTokenizer

    CALL a.clear()
    LET tok = base.StringTokenizer.create(s,delimiter)
    WHILE tok.hasMoreTokens()
        LET a[a.getLength()+1] = tok.nextToken()
    END WHILE
    -- a is DYNAMIC ARRAY so has been pased by reference and does not need to be explicitly returned
END FUNCTION
fourjs.reuben
  • 286
  • 3
  • 3