2015-05-14 541 views
0

我想知道的Informix 4GL命令分割變量 如Informix的4GL拆分一個字符串或字符

lv_var = variable01;variable02 

lv_var01 = variable01 
lv_var02 = variable02 

有什麼Informix中的4GL可以做這件事。

在Python中我可以做

lv_array = lv_var.split(";") 

,並從陣列

回答

0

沒有一個標準功能,要做到這一點使用的變量。一個主要問題是返回數組。我可能會寫一個C函數來完成這項工作,但在I4GL,它看起來像:

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 
0

什麼,你會發現,已經經過多年的成長如superceed的Informix 4GL產品FourJs Genero將會添加內置的方法來簡化Informix 4GL開發人員的生活。

所以像這樣的你在找什麼,如果你升級到的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 
1

有可能與經典的Informix 4GL有這樣的事情...

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 

日期:

a 
b 
c 
d 

動態陣列支持需要IBM Informix 4GL 7.32.UC1或更高版本