;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfArrayFileGetDim2 (strFilename, strSepCol, intStartRow, intStartCol) If FileSizeEx (strFilename, 0) < 1 Then Return ArrDimension (0) ; Return valid dim-0 array with no element. arrA1 = ArrayFileGet (strFilename) If strSepCol == "" Then Return arrA1 ; Return dim-1 array. strSepCol = StrSub (strSepCol, 1, 1) intElems1 = ArrInfo (arrA1, 1) intElems2 = ItemCount (arrA1[0], strSepCol) If intElems2 == 1 Then Return arrA1 ; Return dim-1 array. intStartRow = Max (0, intStartRow) intStartCol = Max (0, intStartCol) arrA2 = ArrDimension (intElems1 + intStartRow, intElems2 + intStartCol) ArrInitialize (arrA2, "") For intRow = 1 To intElems1 strRow = arrA1[intRow - 1] For intCol = 1 To intElems2 arrA2[intRow - 1 + intStartRow, intCol - 1 + intStartCol] = ItemExtract (intCol, strRow, strSepCol) Next Next Return arrA2 ; Return dim-2 array. ;.......................................................................................................................................... ; This UDF "udfArrayFileGetDim2" creates a dim-2 array from a given dim-1 array. ; ; The input array is treated as a dim-1 array, which holds a list of dim-1 arrays, ; each represented as a serialized string list. ; The dim-2 elements are delimited by the given column separator character. ; ; If the given file is empty, then the function returns a valid dim-0 array with no element. ; ; If the column separator is empty, then the function returns the entire dim-1 input array, ; If there are no dim-2 elements, then the function returns the entire dim-1 input array, ; ; Sometimes it may be useful to work with an array which starts from the origin (1,1) instead (0,0), ; so parameters intStartRow and intStartCol can be used to shift the origin of the data area ; in the newly created dim-2 array, to make place for additionally data in rows or columns or both. ; ; Syntax: ; arr:Array = udfArrayFileGetDim2 (str:Filename, str:SepCol, int:StartRow, int:StartCol) ;.......................................................................................................................................... ; Detlev Dalitz.20110226. ; Adapted from a proposal by Lars M. Doornbos.20110226. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. DirChange (DirScript ()) GoSub DEFINE_FUNCTIONS strFileTemp = ShortCutDir ("Local Settings", 0, 1) : "Temp\" : "WB." : StrReplace (TimeYmdHms (), ":", "") : ".txt" :Test11 strList = StrReplace ("1|apple@2|pear@3|banana", "@", @CRLF) FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 0 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 3 x 2 = 6 Elements. GoSub DisplayResult :Test12 strList = StrReplace ("1|apple@2|pear@3|banana@", "@", @CRLF) ; Trailing dim-1 delimiter will be ommitted by ArrayFileGet function. FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 0 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 3 x 2 = 6 Elements. GoSub DisplayResult :Test21 strList = StrReplace ("1|apple@2|pear@3|banana", "@", @CRLF) FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 1 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 4 x 2 = 8 Elements. GoSub DisplayResult :Test22 strList = StrReplace ("1|apple@2|pear@3|banana@", "@", @CRLF) ; Trailing dim-1 delimiter will be ommitted by ArrayFileGet function. FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 1 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 4 x 2 = 8 Elements. GoSub DisplayResult :Test31 strList = StrReplace ("1|apple@2|pear@3|banana", "@", @CRLF) FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 1 intStartCol = 1 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 4 x 3 = 12 Elements. GoSub DisplayResult :Test32 strList = StrReplace ("1|apple@2|pear@3|banana@", "@", @CRLF) ; Trailing dim-1 delimiter will be ommitted by ArrayFileGet function. FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 1 intStartCol = 1 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 4 x 3 = 12 Elements. GoSub DisplayResult :Test41 strList = "" FilePut (strFileTemp, strList) ; Create dim-1 array file. strSepCol = "|" intStartRow = 0 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 0 x 0 = 0 Elements. GoSub DisplayResult :Test51 strList = "1" FilePut (strFileTemp, strList) strSepCol = "|" intStartRow = 0 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 1 x 1 = 1 Element. GoSub DisplayResult :Test61 strList = StrReplace ("1|apple@2|pear@3|banana", "@", @CRLF) FilePut (strFileTemp, strList) strSepCol = "" intStartRow = 0 intStartCol = 0 arrArray = udfArrayFileGetDim2 (strFileTemp, strSepCol, intStartRow, intStartCol) ; 3 x 1 = 3 Elements. GoSub DisplayResult If FileExist (strFileTemp) == 1 Then FileDelete (strFileTemp) Exit ;------------------------------------------------------------------------------------------------------------------------------------------ :DisplayResult strMsgTitle = "Demo: udfArrayizeDim2 (strList, strSepRow, strSepCol, intStartRow, intStartCol)" strFilename = ShortCutDir ("Local Settings", 0, 1) : "Temp\Demo.udfArrayizeDim2.txt" strWindowNP = "" If udfArrayUnloadToFile (arrArray, strFilename) blnResult = RunZoom ("notepad.exe", strFilename) strWindowNP = WinGetactive () blnResult = FileDelete (strFilename) EndIf intDims = ArrInfo (arrArray, 0) ; Number of dimensions in the array. intElements = ArrInfo (arrArray, 6) ; Number of elements in the entire array. strMsgText = "" ;strMsgText = "strList = " : strList : @LF ;strMsgText = strMsgText : "strSepRow = " : strSepRow : @LF strMsgText = strMsgText : "strSepCol = " : strSepCol : @LF strMsgText = strMsgText : "intDims = " : intDims : @LF strMsgText = strMsgText : "intElements = " : intElements : @LF IntControl (63, 300, 300, 700, 700) ; Sets coordinates for AskFileText, AskItemList and AskTextBox windows. IntControl (28, 1, 0, 0, 0) ; Selects system font used in list boxes. p1=1=fixed pitch font. AskItemlist (strMsgTitle, strMsgText, @LF, @UNSORTED, @SINGLE) :CANCEL If IsDefined (strWindowNP) Then If strWindowNP != "" Then If WinExist (strWindowNP) Then WinClose (strWindowNP) Return ; from GoSub DisplayResult. ;------------------------------------------------------------------------------------------------------------------------------------------ ;========================================================================================================================================== :DEFINE_FUNCTIONS ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfArrayUnloadToFile (arrArray, strFilename) If !ArrInfo (arrArray, -1) Then Return 0 ; No array. If !ArrInfo (arrArray, 6) Then Return 0 ; No elements. arrArrInfo = ArrDimension (8) arrArrInfo[0] = "ArrInfo;-1;{0};Validity of the array. 1=@TRUE valid, 0=@FALSE invalid." arrArrInfo[1] = "ArrInfo;0;{1};Number of dimensions in the array." arrArrInfo[2] = "ArrInfo;1;{2};Number of elements in dimension 1." arrArrInfo[3] = "ArrInfo;2;{3};Number of elements in dimension 2." arrArrInfo[4] = "ArrInfo;3;{4};Number of elements in dimension 3." arrArrInfo[5] = "ArrInfo;4;{5};Number of elements in dimension 4." arrArrInfo[6] = "ArrInfo;5;{6};Number of elements in dimension 5." arrArrInfo[7] = "ArrInfo;6;{7};Number of elements in the entire array." intDims = ArrInfo (arrArray, 0) strIndexFill = StrFill (",0", 2 * (5 - intDims)) arrD = ArrDimension (6) arrE = ArrDimension (6) For intD = 1 To 5 arrE[intD] = Max (ArrInfo (arrArray, intD) - 1, 0) Next hdlFW = FileOpen (strFilename, "WRITE") strBOM = "" ; BOM (EF BB BF) for Unicode UTF-8. FileWrite (hdlFW, strBOM : '<?xml version="1.0" encoding="utf-8" standalone="yes"?>') ; XML declaration line with leading BOM (EF BB BF). FileWrite (hdlFW, "<ARRAY>") ; Open node "ARRAY". FileWrite (hdlFW, "<ARRINFO><![CDATA[") ; Open node "ARRINFO". ; Write data. For intI = 0 To 7 FileWrite (hdlFW, StrReplace (arrArrInfo[intI], "{" : intI : "}", ArrInfo (arrArray, intI - 1))) Next FileWrite (hdlFW, "]]></ARRINFO>") ; Close node "ARRINFO". FileWrite (hdlFW, "<ARRDATA><![CDATA[") ; Open node "ARRDATA". ; Write data. For intD1 = 0 To arrE[1] arrD[1] = intD1 For intD2 = 0 To arrE[2] arrD[2] = intD2 For intD3 = 0 To arrE[3] arrD[3] = intD3 For intD4 = 0 To arrE[4] arrD[4] = intD4 For intD5 = 0 To arrE[5] arrD[5] = intD5 strIdx = "" For intD = 1 To intDims strIdx = ItemInsert (arrD[intD], -1, strIdx, ",") Next strArrIndex = strIdx : strIndexFill intVarType = VarType (arrArray [%strIdx%]) If intVarType strOut = ChrStringToUnicode ("" : arrArray [%strIdx%]) intPrevCodePage = ChrSetCodepage (65001) FileWrite (hdlFW, strArrIndex : ";" : intVarType : ";" : strOut) ChrSetCodepage (intPrevCodePage) Else FileWrite (hdlFW, strArrIndex : ";" : intVarType : ";") EndIf Next Next Next Next Next FileWrite (hdlFW, "]]></ARRDATA>") ; Close node "ARRDATA". FileWrite (hdlFW, "</ARRAY>") ; Close node "ARRAY". hdlFW = FileClose (hdlFW) Return FileSizeEx (strFilename) ;.......................................................................................................................................... ; This function "udfArrayUnloadToFile" creates a specific array definition textfile (xml) from array, ; which can be used to load data back into an array by function "udfArrayLoadFromFile". ; ; Detlev Dalitz.20010731.20020828.20030222.20090528.20100122.20100125.20110123. ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ Return ; from GoSub DEFINE_FUNCTIONS ;==========================================================================================================================================