1 頁 (共 1 頁)

nested array to string

發表於 : 2024-05-22, 23:08
admin
demo.prg:

代碼: 選擇全部

func test
local a := {1,"A", {.F., Date(), 3}, {'B',{'C',2,5},4}}
local c,d
msginfo( c := ArrayToStr(a) )

d := &(c)

c := ArrayToStr(d)
msgInfo(c)

return 
function:

代碼: 選擇全部

/*
  author: WenSheng(Taiwan)
  name: 陣列轉字串,支援巢狀陣列
*/
Func    ArrayToStr(a)
LOCAL   i := 0
LOCAL   c := ''
LOCAL   v := NIL
        FOR i := 1 To Len(a)
            IF i == 1
               c += '{'
            ENDIF
            //
            IF i > 1
               c += ','
            ENDIF
            //
            v :=  ValType( a[i] )
            IF v == 'A'
               c += ArrayToStr(a[i])
            ELSE
               DO CASE
                  CASE v == "C" .OR.;
                       v == "M"
                       c += ["]+a[i]+["]
                  CASE v == "N"
                       c += hb_ntos( a[i] )
                  CASE v == "D"
                       c += ["]+DToS( a[i] )+["]
                  CASE v == "T"
                       c += ["]+hb_TToS( a[i] )+["]
                  CASE v == "L"
                       c += iif( a[i], ".T.", ".F." )
               ENDCASE
            ENDIF
            //
            IF i == Len(a)
               c += '}'
            ENDIF
        NEXT i
RETURN c