udfArrayToList
str udfArrayToList (arr, str)
;----------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayToList (arrArray, strDelimiter)
If !ArrInfo (arrArray, -1) Then Return "" ; No array.
If !ArrInfo (arrArray, 6) Then Return "" ; No elements.

arrE = ArrDimension (6)
arrE [0] = ArrInfo (arrArray, 0)
For intI = 1 To 5
   arrE [intI] = Max (0, ArrInfo (arrArray, intI) - 1)
Next

arrD = ArrDimension (6)
ArrInitialize (arrD, "")
intLen = Min (arrE [0], StrLen (strDelimiter))
For intI = 1 To intLen
   arrD [intI] = StrSub (strDelimiter, intI, 1)
Next

Switch arrE [0]
Case 1
   strItemList1 = ""
   For intD1 = 0 To arrE [1]
      If !!VarType (arrArray [intD1])
         strItemList1 = strItemList1 : arrD [1] : arrArray [intD1]
      Else
         strItemList1 = strItemList1 : arrD [1]
      EndIf
   Next
   If arrD [1] != "" Then strItemList1 = StrSub (strItemList1, 2, -1)
   Break
Case 2
   strItemList1 = ""
   For intD1 = 0 To arrE [1]
      strItemList2 = ""
      For intD2 = 0 To arrE [2]
         If !!VarType (arrArray [intD1, intD2])
            strItemList2 = strItemList2 : arrD [2] : arrArray [intD1, intD2]
         Else
            strItemList2 = strItemList2 : arrD [2]
         EndIf
      Next
      If arrD [2] != "" Then strItemList2 = StrSub (strItemList2, 2, -1)
      strItemList1 = strItemList1 : arrD [1] : strItemList2
   Next
   If arrD [1] != "" Then strItemList1 = StrSub (strItemList1, 2, -1)
   Break
Case 3
   strItemList1 = ""
   For intD1 = 0 To arrE [1]
      strItemList2 = ""
      For intD2 = 0 To arrE [2]
         strItemList3 = ""
         For intD3 = 0 To arrE [3]
            If !!VarType (arrArray [intD1, intD2, intD3])
               strItemList3 = strItemList3 : arrD [3] : arrArray [intD1, intD2, intD3]
            Else
               strItemList3 = strItemList3 : arrD [3]
            EndIf
         Next
         If arrD [3] != "" Then strItemList3 = StrSub (strItemList3, 2, -1)
         strItemList2 = strItemList2 : arrD [2] : strItemList3
      Next
      If arrD [2] != "" Then strItemList2 = StrSub (strItemList2, 2, -1)
      strItemList1 = strItemList1 : arrD [1] : strItemList2
   Next
   If arrD [1] != "" Then strItemList1 = StrSub (strItemList1, 2, -1)
   Break
Case 4
   strItemList1 = ""
   For intD1 = 0 To arrE [1]
      strItemList2 = ""
      For intD2 = 0 To arrE [2]
         strItemList3 = ""
         For intD3 = 0 To arrE [3]
            strItemList4 = ""
            For intD4 = 0 To arrE [4]
               If !!VarType (arrArray [intD1, intD2, intD3, intD4])
                  strItemList4 = strItemList4 : arrD [4] : arrArray [intD1, intD2, intD3, intD4]
               Else
                  strItemList4 = strItemList4 : arrD [4]
               EndIf
            Next
            If arrD [4] != "" Then strItemList4 = StrSub (strItemList4, 2, -1)
            strItemList3 = strItemList3 : arrD [3] : strItemList4
         Next
         If arrD [3] != "" Then strItemList3 = StrSub (strItemList3, 2, -1)
         strItemList2 = strItemList2 : arrD [2] : strItemList3
      Next
      If arrD [2] != "" Then strItemList2 = StrSub (strItemList2, 2, -1)
      strItemList1 = strItemList1 : arrD [1] : strItemList2
   Next
   If arrD [1] != "" Then strItemList1 = StrSub (strItemList1, 2, -1)
   Break
Case 5
   strItemList1 = ""
   For intD1 = 0 To arrE [1]
      strItemList2 = ""
      For intD2 = 0 To arrE [2]
         strItemList3 = ""
         For intD3 = 0 To arrE [3]
            strItemList4 = ""
            For intD4 = 0 To arrE [4]
               strItemList5 = ""
               For intD5 = 0 To arrE [5]
                  If !!VarType (arrArray [intD1, intD2, intD3, intD4, intD5])
                     strItemList5 = strItemList5 : arrD [5] : arrArray [intD1, intD2, intD3, intD4, intD5]
                  Else
                     strItemList5 = strItemList5 : arrD [5]
                  EndIf
               Next
               If arrD [5] != "" Then strItemList5 = StrSub (strItemList5, 2, -1)
               strItemList4 = strItemList4 : arrD [4] : strItemList5
            Next
            If arrD [4] != "" Then strItemList4 = StrSub (strItemList4, 2, -1)
            strItemList3 = strItemList3 : arrD [3] : strItemList4
         Next
         If arrD [3] != "" Then strItemList3 = StrSub (strItemList3, 2, -1)
         strItemList2 = strItemList2 : arrD [2] : strItemList3
      Next
      If arrD [2] != "" Then strItemList2 = StrSub (strItemList2, 2, -1)
      strItemList1 = strItemList1 : arrD [1] : strItemList2
   Next
   If arrD [1] != "" Then strItemList1 = StrSub (strItemList1, 2, -1)
   Break
EndSwitch
Return strItemList1
;----------------------------------------------------------------------------------------------------------------------
; This UDF "udfArrayToList" converts a given array into a serialized itemlist
; with each item separated by the delimiter character accordingly to the current dimension.
;
; Example: strMyItemList = udfArrayToList (arrMyArray, @TAB)
; Creates an itemlist from array.
;
; Note:
; This UDF supports dim-1 to dim-5 array.
; An array element which is not initialized has a Vartype=0 (undefined).
; Therefore an empty item will be appended to target itemlist.
;
; Detlev Dalitz.20020718.20090519.20110923.
;----------------------------------------------------------------------------------------------------------------------
#EndFunction
;----------------------------------------------------------------------------------------------------------------------


; Test

GoSub Test1

GoSub Test2

GoSub Test3

GoSub Test4

GoSub Test5

Exit


;==========================================================================================================================================
:Test1
;==========================================================================================================================================
arrArray = ArrDimension (2, 3, 4, 3, 2)
ArrInitialize (arrArray, "o")


strDelims = ""
strItemList = udfArrayToList (arrArray, strDelims)

intCount10 = StrCnt (strItemList, "o", 1, -1, @TRUE)                       ; 144
intCount11 = StrCnt (strItemList, StrSub (strDelims, 1, 1), 1, -1, @TRUE)  ; 0
intCount12 = StrCnt (strItemList, StrSub (strDelims, 2, 1), 1, -1, @TRUE)  ; 0
intCount13 = StrCnt (strItemList, StrSub (strDelims, 3, 1), 1, -1, @TRUE)  ; 0
intCount14 = StrCnt (strItemList, StrSub (strDelims, 4, 1), 1, -1, @TRUE)  ; 0
intCount15 = StrCnt (strItemList, StrSub (strDelims, 5, 1), 1, -1, @TRUE)  ; 0
; strdelims = ""
; "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"


strDelims = @TAB
strItemList = udfArrayToList (arrArray, strDelims)

intCount20 = StrCnt (strItemList, "o", 1, -1, @TRUE)                       ; 144
intCount21 = StrCnt (strItemList, StrSub (strDelims, 1, 1), 1, -1, @TRUE)  ; 1
intCount22 = StrCnt (strItemList, StrSub (strDelims, 2, 1), 1, -1, @TRUE)  ; 0
intCount23 = StrCnt (strItemList, StrSub (strDelims, 3, 1), 1, -1, @TRUE)  ; 0
intCount24 = StrCnt (strItemList, StrSub (strDelims, 4, 1), 1, -1, @TRUE)  ; 0
intCount25 = StrCnt (strItemList, StrSub (strDelims, 5, 1), 1, -1, @TRUE)  ; 0
; strDelims = @TAB
; "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo@TABoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"


strDelims = @TAB : "|"
strItemList = udfArrayToList (arrArray, strDelims)

intCount30 = StrCnt (strItemList, "o", 1, -1, @TRUE)                       ; 144
intCount31 = StrCnt (strItemList, StrSub (strDelims, 1, 1), 1, -1, @TRUE)  ; 1
intCount32 = StrCnt (strItemList, StrSub (strDelims, 2, 1), 1, -1, @TRUE)  ; 4
intCount33 = StrCnt (strItemList, StrSub (strDelims, 3, 1), 1, -1, @TRUE)  ; 0
intCount34 = StrCnt (strItemList, StrSub (strDelims, 4, 1), 1, -1, @TRUE)  ; 0
intCount35 = StrCnt (strItemList, StrSub (strDelims, 5, 1), 1, -1, @TRUE)  ; 0
; strDelims = StrCat (@TAB:"|")
; "oooooooooooooooooooooooo|oooooooooooooooooooooooo|oooooooooooooooooooooooo@TABoooooooooooooooooooooooo|oooooooooooooooooooooooo|oooooooooooooooooooooooo"


strDelims = @TAB : "|/=+"
strItemList = udfArrayToList (arrArray, strDelims)

intCount40 = StrCnt (strItemList, "o", 1, -1, @TRUE)                       ; 144
intCount41 = StrCnt (strItemList, StrSub (strDelims, 1, 1), 1, -1, @TRUE)  ;   1       ==>   2 elements (dim1 = 2)
intCount42 = StrCnt (strItemList, StrSub (strDelims, 2, 1), 1, -1, @TRUE)  ;   4  +  2 ==>   6 elements (dim2 = 3)
intCount43 = StrCnt (strItemList, StrSub (strDelims, 3, 1), 1, -1, @TRUE)  ;  18  +  6 ==>  24 elements (dim3 = 4)
intCount44 = StrCnt (strItemList, StrSub (strDelims, 4, 1), 1, -1, @TRUE)  ;  48  + 24 ==>  72 elements (dim4 = 3)
intCount45 = StrCnt (strItemList, StrSub (strDelims, 5, 1), 1, -1, @TRUE)  ;  72  + 72 ==> 144 elements (dim5 = 2)
; strDelims = StrCat (@TAB:"|/=+")
; "o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o|o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o|o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o@TABo+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o|o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o|o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o/o+o=o+o=o+o"

Drop (arrArray, strDelims, strItemList)
DropWild ("intCount*")

;==========================================================================================================================================
Return ; from Gosub Test1
;==========================================================================================================================================



;==========================================================================================================================================
:Test2
;==========================================================================================================================================
strMsgTitle = "Demo Test2: udfArrayToList (arrArray, strDelimiter)"

strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this file as test input.

; Count lines.
intLineCount = 0
hdlFR = FileOpen (strFilename, "READ")
While @TRUE
   If FileRead (hdlFR) == "*EOF*" Then Break
   intLineCount = intLineCount + 1
EndWhile
hdlFR = FileClose (hdlFR)

; Define a dim-2 array.
arrMyArray = ArrDimension (intLineCount, 5) ; 2nd dimension is oversized, may contain not initialized elements
Message (strMsgTitle, "MyArray contains " : ArrInfo (arrMyArray, 6) : " elements.")

; Fill the array with data from this file.
intLineCount = 0
hdlFR = FileOpen (strFilename, "READ")
While @TRUE
   strLine = FileRead (hdlFR)
   If strLine == "*EOF*" Then Break
   arrMyArray [intLineCount, 0] = intLineCount + 1   ; Line number.
   arrMyArray [intLineCount, 1] = strLine            ; Line content.
   ; arrMyArray [intLineCount,3]                     ; Leave this cell NOT initialized.
   arrMyArray [intLineCount, 3] = Random (99999)     ; Any random number.
   arrMyArray [intLineCount, 4] = StrFill ("=", 10)  ; Just a string of chars.
   intLineCount = intLineCount + 1
EndWhile
hdlFR = FileClose (hdlFR)

; Convert the dim-2 array to a structured itemlist.
strMyItemList = udfArrayToList (arrMyArray, @TAB : "|")

intItemCount = ItemCount (strMyItemList, @TAB)
Message (strMsgTitle, "MyItemList contains " : intItemCount : " items (array dim-1 elements).")

; Display result.
IntControl (28, 1, 0, 0, 0)
IntControl (63, 100, 100, 900, 900)
AskItemlist (strMsgTitle, strMyItemList, @TAB, @UNSORTED, @SINGLE)


:CANCEL

Drop (arrMyArray, hdlFR, intItemCount, intLineCount, strFilename, strLine, strMsgTitle, strMyItemList)

;==========================================================================================================================================
Return ; from Gosub Test2
;==========================================================================================================================================



;==========================================================================================================================================
:Test3
;==========================================================================================================================================
strMsgTitle = "Demo Test3: udfArrayToList (arrArray, strDelimiter)"


strFileList = ShortCutDir ("Personal", 0, @TRUE) : "*.txt"
arrFI = FileInfoToArray (strFileList, 1 | 0)

; Convert the dim-2 array to a structured itemlist.
strMyItemList = udfArrayToList (arrFI, @LF : "|")

intItemCount = ItemCount (strMyItemList, @LF)
Message (strMsgTitle, "MyItemList contains " : intItemCount : " items (array dim-1 elements).")

; Display result.
IntControl (28, 1, 0, 0, 0)
IntControl (63, 000, 050, 1000, 950)
AskItemlist (strMsgTitle, strMyItemList, @LF, @UNSORTED, @SINGLE)

:CANCEL

Drop (arrFI, intItemCount, strFileList, strMsgTitle, strMyItemList)

;==========================================================================================================================================
Return ; from Gosub Test3
;==========================================================================================================================================



;==========================================================================================================================================
:Test4
;==========================================================================================================================================
strMsgTitle = "Demo Test4: udfArrayToList (arrArray, strDelimiter)"

arrYmdHms = Arrayize (TimeYmdHms (), ":")

; Convert the dim-1 array to a simple itemlist.
strMyItemList = udfArrayToList (arrYmdHms, "-")

intItemCount = ItemCount (strMyItemList, @LF)
Message (strMsgTitle, "MyItemList contains " : intItemCount : " items (array dim-1 elements).")

; Display result.
IntControl (28, 1, 0, 0, 0)
IntControl (63, 300, 300, 700, 700)
AskItemlist (strMsgTitle, strMyItemList, @LF, @UNSORTED, @SINGLE)

:CANCEL

Drop (arrYmdHms, intItemCount, strMsgTitle, strMyItemList)

;==========================================================================================================================================
Return ; from Gosub Test4
;==========================================================================================================================================



;==========================================================================================================================================
:Test5
;==========================================================================================================================================
strMsgTitle = "Demo Test5: udfArrayToList (arrArray, strDelimiter)"

arrArray = ArrDimension (2, 3, 4, 3, 2) ; 2*3*4*3*2=144 elements.

For intD1 = 0 To 1
   For intD2 = 0 To 2
      For intD3 = 0 To 3
         For intD4 = 0 To 2
            For intD5 = 0 To 1
               strIdx = ""
               For intD = 1 To 5
                  strIdx = ItemInsert (intD%intD%, -1, strIdx, ",")
               Next
               arrArray [%strIdx%] = strIdx
            Next
         Next
      Next
   Next
Next

; Convert the dim-5 array to dim-1 itemlist.
strDelims = StrFill (@LF, 5)
strMyItemList = udfArrayToList (arrArray, strDelims)

intItemCount = ItemCount (strMyItemList, @LF)
Message (strMsgTitle, "MyItemList contains " : intItemCount : " items (array dim-1 elements).")

; Display result.
IntControl (28, 1, 0, 0, 0)
IntControl (63, 300, 300, 700, 700)
AskItemlist (strMsgTitle, strMyItemList, @LF, @UNSORTED, @SINGLE)

:CANCEL

DropWild ("intD*")
Drop (arrArray, intItemCount, strDelims, strIdx, strMsgTitle, strMyItemList)

;==========================================================================================================================================
Return ; from Gosub Test5
;==========================================================================================================================================