nested array to string

xBase/clipper
回覆文章
admin
Site Admin
文章: 53
註冊時間: 2014-09-23, 10:58

nested array to string

文章 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
line ID: ssbbstw
WeChat ID: ssbbstw
回覆文章