Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version

WinBatch Scripting - Itemlist and Files



Seitenanfang/TopOfPage Seitenende/EndOfPage
Seitenanfang/TopOfPage Seitenende/EndOfPage

Itemlist Functions

;==========================================================================================================================================
; UDF's collected, revised, generated by Detlev Dalitz.
; Revisions: 20010630.20020321.20020517.20020628.20020817.20020821.20020915.20021126.20030102.20030706.
;------------------------------------------------------------------------------------------------------------------------------------------
; Some itemsort methods were contributed by Craig Storey - cstorey@canada.com 20010411.
; See Article ID W15009 on WindowWare Tech Database.
;==========================================================================================================================================


;==========================================================================================================================================
; Overview of itemlist functions
;------------------------------------------------------------------------------------------------------------------------------------------
; udfItemLocateNC (sItem, sItemList, sDelimiter)           ; Returns position of Item in ItemList, or 0 if no match found, ignoring case.
; udfItemFetch (sItem, sItemList, sDelimiter, iMatchCase)  ; Returns target Item as exists in ItemList or "" if Item is not in ItemList.
; udfItemLocateWild (sPattern, sItemList, sDelimiter)      ; Returns ItemList of items found.
;                                                          ; In the wildcard sPattern, "*" matches zero or more characters,
;                                                          ; and "?" matches any one character.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfItemSortDesc (sItemList, sDelimiter)                  ; Returns descending sorted itemlist.
; udfItemSortDesc_2 (sItemList, sDelimiter)                ; Returns descending sorted itemlist (alternative algorithm).
;------------------------------------------------------------------------------------------------------------------------------------------
; udfItemSortNum (sItemList, sDelimiter, iDirection)       ; Returns numerical sorted itemlist of integer numbers.
; udfItemSortNumArr (sItemList, sDelimiter, iDirection)    ; Returns numerical sorted itemlist of integer and/or floating point numbers.
; udfItemSortFlt (sItemList, sDelimiter, iDirection)       ; Returns numerical sorted itemlist of integer and/or floating point numbers.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfReverseList (sItemList, sDelimiter)                   ; Returns reversed itemlist.
; udfReverseList_2 (sItemList, sDelimiter)                 ; Returns reversed itemlist (alternative algorithm).
;------------------------------------------------------------------------------------------------------------------------------------------
; udfIniToList (sIniFilename, sSection, sDelimiter)        ; Create itemlist from one or all sections of an inifile.
;
; udfFileToItemList (sFilename, sDelimiter)                ; Create itemlist from textfile, e.g. MyItemList = udfFileToItemList(myfile,@TAB)
; udfItemListToFile (sItemList, sDelimiter, sFilename)     ; Create textfile from ItemList, e.g. udfItemListToFile(mylist,@TAB,myfile)
;
; udfFileToItemList_2 (sFilename, sDelimiter)              ; Create itemlist from textfile, e.g. MyItemList = udfFileToItemList(myfile,@TAB)
; udfItemListToFile_2 (sItemList, sDelimiter, sFilename)   ; Create textfile from ItemList, e.g. udfItemListToFile(mylist,@TAB,myfile)
;
; udfSortTextFile (sFilenameIn, sFilenameOut, iDirection)  ; Creates sorted textfile output from textfile input.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfTrimList (sItemList, sDelimiter)                      ; Returns itemlist without empty items.
; udfTrimListDup (sItemList, sDelimiter)                   ; Returns itemlist without empty items and without duplicates.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfCsvToTab (sCsvList)                                   ; Returns tab delimited itemlist, converts csvlist to tablist.
; udfCsvToTab_1 (sCsvList)                                 ; Slow routine.
; udfCsvToTab_2 (sCsvList)                                 ; Fast routine.
; udfCsvToTab_3 (sCsvList)                                 ; Fast routine, nearly the same as udfCsvToTab_2 but
;                                                          ; replaces double Quotes in text items with unique Quote.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfRandomList_1 (iLow, iHigh, sDelimiter)                ; Returns list with unique random numbers.
; udfRandomList_2 (iLow, iHigh, iStep, sDelimiter)         ; Returns list with unique random numbers in step width.
;------------------------------------------------------------------------------------------------------------------------------------------
; udfExecuteList (sItemList, sDelimiter, iMode)            ; Accepts itemlist of "var=expression", returns itemlist of "var=value".
;==========================================================================================================================================


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemlocatenc",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemlocatenc

#DefineFunction udfItemLocateNC (sItem, sItemList, sDelimiter)
Return (ItemLocate(StrLower(sItem), StrLower(sItemList), sDelimiter))
#EndFunction

:skip_udfitemlocatenc
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemfetch",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemfetch

#DefineFunction udfItemFetch (sItem, sItemList, sDelimiter, iMatchCase)
If Max(0,Min(1,iMatchCase)) Then iPos = ItemLocate(sItem, sItemList, sDelimiter)
   Else iPos = ItemLocate(StrLower(sItem), StrLower(sItemList), sDelimiter)
sItem = ""
If iPos Then sItem = ItemExtract(iPos, sItemList, sDelimiter)
Return (sItem)
; Returns target sItem as exists in sItemList or empty string "" if sItem is not in sItemList.
#EndFunction

:skip_udfitemfetch
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemlocatewild",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemlocatewild

#DefineFunction udfItemLocateWild (sPattern, sItemList, sDelimiter)
sLocateList = ""
iCount = ItemCount(sItemList,sDelimiter)
For i=1 To iCount
   sItem = ItemExtract(i,sItemList,sDelimiter)
   If (StrIndexWild(sItem,sPattern,1) == 1) Then sLocateList = ItemInsert(sItem,-1,sLocateList,sDelimiter)
Next
Return (sLocateList)
; In the wildcard sPattern, "*" matches zero or more characters, and "?" matches any one character.
; Returns itemlist of items found.
#EndFunction

:skip_udfitemlocatewild
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortdesc",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortdesc

#DefineFunction udfItemSortDesc (sItemList, sDelimiter)
sAscList = ItemSort(sItemList,sDelimiter)
iCount = ItemCount(sAscList,sDelimiter)
sDescList = ""
For i=iCount To 1 By -1
   sItem = ItemExtract(i,sAscList,sDelimiter)
   sDescList = ItemInsert(sItem,-1,sDescList,sDelimiter)
Next
Return (sDescList)
; Returns a descending sorted itemlist.
#EndFunction

:skip_udfitemsortdesc
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortdesc_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortdesc_2

#DefineFunction udfItemSortDesc_2 (sItemList, sDelimiter)
sItemList = ItemSort(sItemList,sDelimiter)
iCount = ItemCount(sItemList,sDelimiter)
For i=iCount To 1 By -1
   sItemList = ItemRemove(i,ItemInsert(ItemExtract(i,sItemList,sDelimiter),-1,sItemList,sDelimiter),sDelimiter)
Next
Return (sItemList)
; Returns a descending sorted itemlist.
#EndFunction

:skip_udfitemsortdesc_2
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortnum",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortnum

#DefineFunction udfItemSortNum (sItemList, sDelimiter, iDirection)
iCount = ItemCount(sItemList,sDelimiter)
sTempList = ""
For i=1 To iCount
   sItem = ItemExtract(i,sItemList,sDelimiter)
   sItem = StrFixLeft(sItem,"",16)
   sTempList = ItemInsert(sItem,-1,sTempList,sDelimiter)
Next
sTempList = ItemSort(sTempList,sDelimiter)
sSortList = ""
iIndexPos = -1 * (iDirection == @ASCENDING)
iIndexNeg = -1 * (iDirection == @DESCENDING)
For i=1 To iCount
   sItem = ItemExtract(i,sTempList,sDelimiter)
   sItem = 0+sItem
   If (sItem >= 0) Then sSortList = ItemInsert(sItem,iIndexPos,sSortList,sDelimiter)
      Else sSortList = ItemInsert(sItem,iIndexNeg,sSortList,sDelimiter)
Next
Return (sSortList)
;..........................................................................................................................................
; sItemList = "0,1,-3,5,-7,9,-11,13,15,2,-4,6,-8,10,-12,14"
;
; iDirection = @ASCENDING
; sSortList = "-12,-11,-8,-7,-4,-3,0,1,2,5,6,9,10,13,14,15"
;
; iDirection = @DESCENDING
; sSortList = "15,14,13,10,9,6,5,2,1,0,-3,-4,-7,-8,-11,-12"
;
; Note: Blank char is forbidden to use as sDelimiter.
;
; Detlev Dalitz.20020915
;..........................................................................................................................................
#EndFunction

:skip_udfitemsortnum
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortnumarr",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortnumarr

#DefineFunction udfItemSortNumArr (sItemList, sDelimiter, iDirection)
If (sItemList == "") Then Return ("")
sComp = ">"
If (iDirection == @DESCENDING) Then sComp = "<"
aArray = Arrayize(sItemList,sDelimiter)
ikHigh = Max(0,ArrInfo(aArray,1)-1)
iHigh = ikHigh-1
iLow = 0
For i=iLow To iHigh
   ikLow = i+1
   For k=ikLow To ikHigh
      If (aArray[i] %sComp% aArray[k])
         aA = aArray[i]
         aArray[i] = aArray[k]
         aArray[k] = aA
      EndIf
   Next
Next
sItemList = aArray[0]
For ik=1 To ikHigh
   sItemList = StrCat(sItemList,sDelimiter,aArray[ik])
Next

Return (sItemList)
;..........................................................................................................................................
; This function "udfItemSortNumArr" uses the Array Bubble Sort algorithm.
;
; sItemList = "8,1,2,3,4,5,-1.23,-1,16.00,7.9,7.8,9,10,11,12,13,0,3,-0.1,+0.1"
;
; iDirection = @ASCENDING
; sItemList = "-1.23,-1,-0.1,0,+0.1,1,2,3,3,4,5,7.8,7.9,8,9,10,11,12,13,16.00"
;
; iDirection = @DESCENDING
; sItemList = "16.00,13,12,11,10,9,8,7.9,7.8,5,4,3,3,2,1,+0.1,0,-0.1,-1,-1.23"
;
; Note: Array stores items "as is" but compares items as numbers.
;
; Detlev Dalitz.20030102
;..........................................................................................................................................
#EndFunction

:skip_udfitemsortnumarr
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortflt",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortflt

#DefineFunction udfItemSortFlt (sItemList, sDelimiter, iDirection)
iCount = ItemCount(sItemList,sDelimiter)
hBB = BinaryAlloc(iCount*8)
iHigh = iCount - 1
For i=0 To iHigh
   BinaryPokeFlt(hBB,i*8,ItemExtract(i+1,sItemList,sDelimiter))
Next
BinarySort(hBB,8,0,8,@FLOAT8|iDirection)
sItemList = ""
For i=0 To iHigh
   sItem = BinaryPeekFlt(hBB,i*8)
   sInt = Int(sItem)
   If (sInt == sItem) Then sItemList = ItemInsert(sInt,-1,sItemList,sDelimiter)
      Else sItemList = ItemInsert(sItem,-1,sItemList,sDelimiter)
Next
BinaryFree(hBB)
Return (sItemList)
;..........................................................................................................................................
; sItemList = "8,1,5.5566,-1,300,-34,.1,16,0"
;
; iDirection = @ASCENDING
; sItemList = "-34,-1,0,0.1,1,5.5566,8,16,300"
;
; iDirection = @DESCENDING
; sItemList = "300,16,8,5.5566,1,0.1,0,-1,-34"
;
; Detlev Dalitz.20020915
;..........................................................................................................................................
#EndFunction

:skip_udfitemsortflt
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfreverselist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfreverselist

#DefineFunction udfReverseList (sItemList, sDelimiter)
sReverseList = ""
iCount = ItemCount(sItemList,sDelimiter)
For i=iCount To 1 By -1
   sItem = ItemExtract(i,sItemList,sDelimiter)
   sReverseList = ItemInsert(sItem,-1,sReverseList,sDelimiter)
Next
Return (sReverseList)
#EndFunction

:skip_udfreverselist
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfreverselist_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfreverselist_2

#DefineFunction udfReverseList_2 (sItemList, sDelimiter)
iCount = ItemCount(sItemList,sDelimiter)
For i=iCount To 1 By -1
   sItemList = ItemRemove(i,ItemInsert(ItemExtract(i,sItemList,sDelimiter),-1,sItemList,sDelimiter),sDelimiter)
Next
Return (sItemList)
#EndFunction

:skip_udfreverselist_2
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfinitolist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfinitolist

#DefineFunction udfIniToList (sIniFilename, sSection, sDelimiter)
sSectionsList = sSection
If (sSection == "") Then sSectionsList = IniItemizePvt("",sIniFilename)
iSectionsCount = ItemCount(sSectionsList,@TAB)
sIniList = ""
For iCount=1 To iSectionsCount
   sSection = ItemExtract(iCount,sSectionsList,@TAB)
   sItem = StrCat("[", sSection, "]")
   sIniList = ItemInsert(sItem,-1,sIniList,sDelimiter)
   sKeywordsList = IniItemizePvt(sSection,sIniFilename)
   iKeywordsCount = ItemCount(sKeywordsList,@TAB)
   For kCount=1 To iKeywordsCount
      sKeyword = ItemExtract(kCount,sKeywordsList,@TAB)
      sValue = IniReadPvt(sSection,sKeyword,"???",sIniFilename)
      sItem = StrCat(sKeyword,"=",sValue)
      sIniList = ItemInsert(sItem,-1,sIniList,sDelimiter)
   Next
   sIniList = StrCat(sIniList,sDelimiter) ; Add "empty ruler". This line may be commented out.
Next
Return (sIniList)
;..........................................................................................................................................
; From:  rayche raymond.chevalier@vigilance.ca
; Date:  Tuesday, November 26, 2002 01:49 AM
; Conf:  WinBatch
;
; Slightly modified by Detlev Dalitz.20021126
;..........................................................................................................................................
#EndFunction

:skip_udfinitolist
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffiletoitemlist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffiletoitemlist

#DefineFunction udfFileToItemList (sFilename, sDelimiter)
iBBSize = FileSize(sFilename)
If !iBBSize Then Return ("")
hBB = BinaryAlloc(iBBSize)
BinaryRead(hBB,sFilename)
sItemList = BinaryPeekStr(hBB,0,iBBSize)
BinaryFree(hBB)
sItemList = StrReplace(sItemList,@CRLF,@CR)
sItemList = StrReplace(sItemList,@LF,@CR)
sItemList = StrReplace(sItemList,@CR,sDelimiter)
Return (sItemList)
; Create itemlist from txtfile with EOL=CR or EOL=LF or EOL=CRLF
; Example: MyItemList = udfFileToItemList(myfile,@TAB)
#EndFunction

:skip_udffiletoitemlist
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemlisttofile",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemlisttofile

#DefineFunction udfItemListToFile (sItemList, sDelimiter, sFilename)
If (sItemList=="") Then Return (0)
sItemList = StrReplace(sItemList,sDelimiter,@CRLF)
hBB = BinaryAlloc(StrLen(sItemList))
BinaryPokeStr(hBB,0,sItemList)
iResult = BinaryWrite(hBB,sFilename)
BinaryFree(hBB)
Return (iResult)
; Create txtfile with EOL=CRLF from ItemList
; Example: ItemListToFile(mylist,@TAB,myfile)
#EndFunction

:skip_udfitemlisttofile
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udffiletoitemlist_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udffiletoitemlist_2

#DefineFunction udfFileToItemList_2 (sFilename, sDelimiter)
If !FileSize(sFilename) Then Return ("")
sItemList = FileGet(sFilename)
sItemList = StrReplace(sItemList,@CRLF,@LF)
sItemList = StrReplace(sItemList,@CR,"") ; @CR has no meaning.
If (sDelimiter=="") Then sDelimiter = @TAB
If (sDelimiter!=@LF) Then sItemList = StrReplace(sItemList,@LF,sDelimiter)
Return (sItemList)
; Create itemlist from txtfile with EOL=LF or EOL=CRLF
; Example: MyItemList = udfFileToItemList(myfile,@TAB)
#EndFunction

:skip_udffiletoitemlist_2
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemlisttofile_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemlisttofile_2

#DefineFunction udfItemListToFile_2 (sItemList, sDelimiter, sFilename)
If (sItemList=="") Then Return (0)
sItemList = StrReplace(sItemList,sDelimiter,@CRLF)
iResult = FilePut(sFilename,sItemList)
Return (iResult)
; Create txtfile with EOL=CRLF from ItemList
; Example: ItemListToFile(mylist,@TAB,myfile)
#EndFunction

:skip_udfitemlisttofile_2
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfsorttextfile",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfsorttextfile

#DefineFunction udfSortTextFile (sFilenameIn, sFilenameOut, iDirection)
Select iDirection
Case @ASCENDING
   Return (udfItemListToFile_2(ItemSort(udfFileToItemList_2(sFilenameIn,@LF),@LF),@LF,sFilenameOut))
   Break
Case @DESCENDING
   Return (udfItemListToFile_2(udfReverseList(ItemSort(udfFileToItemList_2(sFilenameIn,@LF),@LF),@LF),@LF,sFilenameOut))
   Break
EndSelect
Return (@FALSE)
#EndFunction

:skip_udfsorttextfile
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udftrimlist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udftrimlist

#DefineFunction udfTrimList (sItemList, sDelimiter)
sTrimList = ""
iCount = ItemCount(sItemList,sDelimiter)
For i=1 To iCount
   sItem = ItemExtract(i,sItemList,sDelimiter)
   If (sItem>"") Then sTrimList = ItemInsert(sItem,-1,sTrimList,sDelimiter)
Next
Return (sTrimList)
#EndFunction

:skip_udftrimlist
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udftrimlistdup",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udftrimlistdup

#DefineFunction udfTrimListDup (sItemList, sDelimiter)
sTrimList = ""
iCount = ItemCount(sItemList,sDelimiter)
For i=1 To iCount
   sItem = ItemExtract(i,sItemList,sDelimiter)
   If (sItem>"") Then If !ItemLocate(sItem,sTrimList,sDelimiter) Then sTrimList = ItemInsert(sItem,-1,sTrimList,sDelimiter)
Next
Return (sTrimList)
#EndFunction

:skip_udftrimlistdup
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcsvtotab_1",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcsvtotab_1

#DefineFunction udfCsvToTab_1 (sCsvList)
; sDelim = ";"  european semicolon separated values
sDelim = ","
sQuote = '"'
sTabList = ""
iInQuote = 0
iStrLen = StrLen(sCsvList)
For i=1 To iStrLen
   sChar = StrSub(sCsvList,i,1)
   If (sChar==sQuote)
      iInQuote = !iInQuote
      Continue
   EndIf
   If ((sChar==sDelim) && !iInQuote) Then sChar = @TAB
   sTabList = StrCat(sTabList,sChar)
Next
Return (sTabList)
;..........................................................................................................................................
; Adapted from WindowWare Tech Database
; Article ID:   W14989
; File Created: 2001:06:19:10:05:45
; Modified by Detlev Dalitz.20020210
;..........................................................................................................................................
#EndFunction

:skip_udfcsvtotab_1
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcsvtotab_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcsvtotab_2

#DefineFunction udfCsvToTab_2 (sCsvList)
; sDelim = ";"  European semicolon separated values.
sDelim = ","
sQuote = '"'
sTabList = ""
If (ItemCount(sCsvList,sQuote)==1)
   sTabList = StrReplace(sCsvList,sDelim,@TAB)
Else
   While (sCsvList>"")
      sItem = ItemExtract(1,sCsvList,sQuote)
      sItem = StrReplace(sItem,sDelim,@TAB)
      sTabList = StrCat(sTabList,sItem)
      sItem = ItemExtract(2,sCsvList,sQuote)
      If (sItem>"") Then sTabList = StrCat(sTabList,sItem) ; Each item without Quotes.
      ; Activate following line alternatively to line above.
      ; If (sItem>"") then sTabList = StrCat(sTabList,sQuote,sItem,sQuote) ; Allow text items having Quotes.
      sCsvList = ItemRemove(1,sCsvList,sQuote)
      sCsvList = ItemRemove(1,sCsvList,sQuote)
   EndWhile
EndIf
Return (sTabList)
#EndFunction

:skip_udfcsvtotab_2
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfcsvtotab_3",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfcsvtotab_3

#DefineFunction udfCsvToTab_3 (sCsvList)
; sDelim = ";"  European semicolon separated values
sDelim = ","
sQuote = '"'
sQuote2 = StrCat(sQuote,sQuote)
sTabList = ""
sCsvList = StrReplace(sCsvList,sQuote2,Num2Char(7)) ; Substitutes following double Quotes with placeholder.
If (ItemCount(sCsvList,sQuote)==1)
   sTabList = StrReplace(sCsvList,sDelim,@TAB)
Else
   While(sCsvList>"")
      sItem = ItemExtract(1,sCsvList,sQuote)
      sItem = StrReplace(sItem,sDelim,@TAB)
      sTabList = StrCat(sTabList,sItem)
      sItem = ItemExtract(2,sCsvList,sQuote)
      If (sItem>"") Then sTabList = StrCat(sTabList,sItem) ; Each item without Quotes.
      ; Activate following line alternatively to line above.
      ; If (sItem>"") then sTabList = StrCat(sTabList,sQuote,sItem,sQuote) ; Allow text items having Quotes.
      sCsvList = ItemRemove(1,sCsvList,sQuote)
      sCsvList = ItemRemove(1,sCsvList,sQuote)
   EndWhile
EndIf
sTabList = StrReplace(sTabList,Num2Char(7),sQuote) ; Replace placeholder with one unique Quote.
Return (sTabList)
;..........................................................................................................................................
; This udf is able to handle strings like
; sCsvList = '111,222,"1a2b, 3ef",123,"dfdfdf", 111, 222,"""Double""Quotes"'
; Double Quotes in text items are replaced by unique Quote, so the tab delimited string looks like
; sTabList='111|222|1a2b, 3ef|123|dfdfdf|111|222|"Double"Quotes' (with |=@TAB)
;
; Conf:  WinBatch 2002C Beta
; From:  Marty marty+bbs@winbatch.com
; Date:  Friday, May 17, 2002 12:01 AM
;..........................................................................................................................................
#EndFunction

:skip_udfcsvtotab_3
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfrandomlist_1",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfrandomlist_1

#DefineFunction udfRandomList_1 (iLow, iHigh, sDelimiter)
sItemList = ""
iRange = iHigh-iLow
iCount = 1+iRange
While (ItemCount(sItemList,sDelimiter)<iCount)
   iNum = iLow+Random(iRange)
   If !ItemLocate(iNum,sItemList,sDelimiter) Then sItemList = ItemInsert(iNum,-1,sItemList,sDelimiter)
EndWhile
Return (sItemList)
#EndFunction

:skip_udfrandomlist_1
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfrandomlist_2",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfrandomlist_2

#DefineFunction udfRandomList_2 (iLow, iHigh, iStep, sDelimiter)
sItemList = ""
iCount = (iHigh-iLow)/Max(iStep,1)
While (ItemCount(sItemList,sDelimiter)<=iCount)
   iNum = iLow+(iStep*(Random(iCount)mod(1+iCount)))
   If !ItemLocate(iNum,sItemList,sDelimiter) Then sItemList = ItemInsert(iNum,-1,sItemList,sDelimiter)
EndWhile
Return (sItemList)
#EndFunction

:skip_udfrandomlist_2
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfexecutelist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfexecutelist

#DefineFunction udfExecuteList (sItemList, sDelimiter, iMode)
sResultList = ""
iCount = ItemCount(sItemList,sDelimiter)
For i=1 To iCount
   sItem = ItemExtract(i,sItemList,sDelimiter)
   sVar  = ItemExtract(1,sItem,"=")
   %sItem%
   Select iMode
   Case 0
      sResultList = ItemInsert(StrCat(sVar,"=",%sVar%),-1,sResultList,sDelimiter)
      Break
   Case 1
      sResultList = ItemInsert(%sVar%,-1,sResultList,sDelimiter)
      Break
   Case iMode
      sResultList = ""
      Break
   EndSelect
Next
Return (sResultList)
;..........................................................................................................................................
; Input parameter sItemList is a list of expression items "var=expression" sDelimited by sDelimiter,
; e.g. sItemList = StrCat("a=1+2",@tab,"b=2**4")
;
; iMode = 0  returns a list of items "var=value"
; iMode = 1  returns a list of items "value" only
;
; Detlev Dalitz.20020318
;..........................................................................................................................................
#EndFunction

:skip_udfexecutelist
;------------------------------------------------------------------------------------------------------------------------------------------

;==========================================================================================================================================



;==========================================================================================================================================
; Tests following ...
;..........................................................................................................................................
; --- test ---  udfItemLocateNC (sItem, sItemList, sDelimiter)
sItemList = "Monday,Sunday,Tuesday"
sItem     = "SuNdAy"
sMsgTitle = "Demo  udfItemLocateNC (sItem, sItemList, sDelimiter)"
sMsgText  = StrCat(sItemList,@CRLF,sItem," = ",udfItemLocateNC(sItem,sItemList,","))
Pause(sMsgTitle,sMsgText)
Drop(sItem,sItemList,sMsgText,sMsgTitle)

;..........................................................................................................................................
; --- test ---  udfItemFetch (sItem, sItemList, sDelimiter, iMatchCase)
sItemList = "Monday,Sunday,FUNday"
sItem1 = udfItemFetch ("funday", sItemList, ",", @TRUE)        ; ""
sItem2 = udfItemFetch ("funday", sItemList, ",", @FALSE)       ; "FUNday"
Drop(sItem1,sItem2,sItemList)

;..........................................................................................................................................
; --- test ---  other examples follows
sObjectList = "apfel,birne,banane,apfelmus,bananenshake,zitrone,orange"
sNumberListInt = "8,1,-1,23,43343,11,-255,111,1111,43,12,13,0,-4711,8"
sNumberListflt = "8,1,5.5566,-1.23,300,-34,.1,16,0,8"

sResultList11 = udfItemLocateWild("ap*",sObjectList,",")       ; "apfel,apfelmus"
sResultList12 = udfItemLocateWild("*ana*",sObjectList,",")     ; "banane,bananenshake"
sResultList13 = udfItemLocateWild("?i*",sObjectList,",")       ; "birne,zitrone"

sResultList21 = ItemSort(sObjectList,",")                      ; "apfel,apfelmus,banane,bananenshake,birne,orange,zitrone"
sResultList22 = udfItemSortDesc(sObjectList,",")               ; "zitrone,orange,birne,bananenshake,banane,apfelmus,apfel"
sResultList23 = udfItemSortDesc_2(sObjectList,",")             ; "zitrone,orange,birne,bananenshake,banane,apfelmus,apfel"

sResultList34 = udfReverseList(sObjectList,",")                ; "orange,zitrone,bananenshake,apfelmus,banane,birne,apfel"
sResultList35 = udfReverseList_2(sObjectList,",")              ; "orange,zitrone,bananenshake,apfelmus,banane,birne,apfel"

sResultList41 = udfItemSortNum(sNumberListInt,",",@ASCENDING)  ; "-4711,-255,-1,0,1,8,8,11,12,13,23,43,111,1111,43343"
sResultList42 = udfItemSortNum(sNumberListInt,",",@DESCENDING) ; "43343,1111,111,43,23,13,12,11,8,8,1,0,-1,-255,-4711"

sResultList51 = udfItemSortNumArr(sNumberListFlt,",",@ASCENDING)  ; "-34,-1.23,0,.1,1,5.5566,8,8,16,300"
sResultList52 = udfItemSortNumArr(sNumberListFlt,",",@DESCENDING) ; "300,16,8,8,5.5566,1,.1,0,-1.23,-34"

sResultList61 = udfItemSortFlt(sNumberListInt,",",@ASCENDING)  ; "-4711,-255,-1,0,1,8,8,11,12,13,23,43,111,1111,43343"
sResultList62 = udfItemSortFlt(sNumberListInt,",",@DESCENDING) ; "43343,1111,111,43,23,13,12,11,8,8,1,0,-1,-255,-4711"
sResultList63 = udfItemSortFlt(sNumberListFlt,",",@ASCENDING)  ; "-34,-1.23,0,0.1,1,5.5566,8,8,16,300"
sResultList64 = udfItemSortFlt(sNumberListFlt,",",@DESCENDING) ; "300,16,8,8,5.5566,1,0.1,0,-1.23,-34"

sResultList71 = udfTrimList(",,apfel,,banane,,apfel,,,apfelmus,,zitrone,orange,,,",",") ; "apfel,banane,apfel,apfelmus,zitrone,orange"
sResultList72 = udfTrimListDup(",orange,apfel,orange,banane,apfelmus,apfel,zitrone,orange,,,",",") ; "orange,apfel,banane,apfelmus,zitrone"

Drop(sObjectList)
DropWild("sNumberList*")
DropWild("sResultList*")

;..........................................................................................................................................
; --- test ---  udfRandomList (low,high,step,sDelimiter)
Pause("Demo  udfRandomList (iLow,iHigh,iStep,sDelimiter)",StrCat('udfRandomList (6,60,6,",")',@CRLF,udfRandomList_2(6,60,6,",")))
Pause("Demo  udfRandomList (iLow,iHigh,iStep,sDelimiter)",StrCat('udfRandomList (3,37,4,"|")',@CRLF,udfRandomList_2(3,37,4,"|")))

;..........................................................................................................................................
; --- test ---  udfExecuteList (sItemList, sDelimiter, iMode)
sMsgTitle = "Demo  udfExecuteList (sItemList, sDelimiter, iMode)"

sExecList = StrCat("a=1+2",@TAB,"b=2**4",@TAB,"c1=a+b",@TAB,"c2=a-b",@TAB,"d=c1*c2")

sMsgText  = StrCat(sExecList,@CRLF,udfExecuteList(sExecList,@TAB,0))
Pause(sMsgTitle,sMsgText)

sMsgText  = StrCat(sExecList,@CRLF,udfExecuteList(sExecList,@TAB,1))
Pause(sMsgTitle,sMsgText)

Drop(sExecList,sMsgText,sMsgTitle)

;..........................................................................................................................................
; --- test --- udfIniToList (sIniFilename, sSection, sDelimiter)
sIniFilename = "win.ini"

sSection = "windows"
sDelimiter = "|"
sIniList = udfIniToList(sIniFilename,sSection,sDelimiter)
sAskItem = AskItemlist(sIniFilename,sIniList,sDelimiter,@UNSORTED,@SINGLE)

sSection = ""
sDelimiter = @LF
sIniList = udfIniToList(sIniFilename,sSection,sDelimiter)
sAskItem = AskItemlist(sIniFilename,sIniList,sDelimiter,@UNSORTED,@SINGLE)

Drop(sIniFilename,sSection,sDelimiter,sIniList,sAskItem)

;..........................................................................................................................................
; --- test ---  udfItemListToFile
; --- test ---  udfFileToItemList
sItemList = udfFileToItemList(IntControl(1004,0,0,0,0),@TAB) ; We use this file as test input.
sAskList = AskItemlist("Choose lines to copy into temp file", sItemList, @TAB, @UNSORTED, @EXTENDED)
sFilenameTemp = FileCreateTemp("TMP")
iResult = udfItemListToFile(sAskList,@TAB,sFilenameTemp)
sItemList = udfFileToItemList(sFilenameTemp,@TAB)
FileDelete(sFilenameTemp)
sAskList = AskItemlist("Check/Review: the lines read from file", sItemList, @TAB, @UNSORTED, @EXTENDED)
Drop(sAskList,iResult,sFilenameTemp,sItemList)

;..........................................................................................................................................
; --- test ---  udfItemListToFile_2
; --- test ---  udfFileToItemList_2
sItemList = udfFileToItemList_2(IntControl(1004,0,0,0,0),@TAB) ; We use this file as test input.
sAskList = AskItemlist("Choose lines to copy into temp file", sItemList, @TAB, @UNSORTED, @EXTENDED)
sFilenameTemp = FileCreateTemp("TMP")
iResult = udfItemListToFile_2(sAskList,@TAB,sFilenameTemp)
sItemList = udfFileToItemList_2(sFilenameTemp,@TAB)
FileDelete(sFilenameTemp)
sAskList = AskItemlist("Check/Review: the lines read from file", sItemList, @TAB, @UNSORTED, @EXTENDED)
Drop(sAskList,iResult,sFilenameTemp,sItemList)

;..........................................................................................................................................
; --- test ---  udfSortTextFile (sFilename)
sFilenameTemp = FileCreateTemp("TMP")

Display(4,"Demo  udfSortTextFile (sFilename)","Hint for the next test: Blank lines are sorted on top of the file!")
udfSortTextFile(IntControl(1004,0,0,0,0),sFilenameTemp,@ASCENDING) ; Read this script and sort it to tempfile.
RunZoomWait("notepad",sFilenameTemp) ; Take a look, be aware that the blank lines are on top of the file.

Display(4,"Demo  udfSortTextFile (sFilename)","Hint for the next test: Blank lines are sorted on bottom of the file!")
udfSortTextFile(sFilenameTemp,sFilenameTemp,@DESCENDING) ; Read tempfile and sort it to tempfile.
RunZoomWait("notepad",sFilenameTemp) ; Take a look and wait for closing notepad.

FileDelete(sFilenameTemp)
Drop(sFilenameTemp)

;..........................................................................................................................................
; --- test ---  udfCsvToTab (sCsvList)
sCsvList1 = '"AUTOSTRADE,4711 ",7.3300,-0.1020,7.4000,7.4400,7.3200,2713500'
sCsvList2 = '7.3300,-0.1020,7.4000,7.4400,7.3200,2713500,"AUTOSTRADE,4711 "'
sCsvList3 = '7.3300,-0.1020,7.4000,7.4400,"AUTOSTRADE,4711 ",7.3200,2713500'
sCsvList4 = '7.3300,-0.1020,7.4000,7.4400,7.3200,2713500'
sCsvList5 = '7.3300,-0.1020,7.4000,7.4400,,7.3200,2713500'
sCsvList6 = '"ACMI.MI","AUTOSTRADE,4711 ",7.3300,"6/21/2001","12:22",-0.1020,7.4000,7.4400,7.3200,2713500'
sResultList1 = udfCsvToTab_3 (sCsvList1)
sResultList2 = udfCsvToTab_3 (sCsvList2)
sResultList3 = udfCsvToTab_3 (sCsvList3)
sResultList4 = udfCsvToTab_3 (sCsvList4)
sResultList5 = udfCsvToTab_3 (sCsvList5)
sResultList6 = udfCsvToTab_3 (sCsvList6)
DropWild("sCsvList*")
DropWild("sResultList*")

;..........................................................................................................................................
; --- test ---  udfCsvToTab (sCsvList)
Display(4,"Demo  udfCsvToTab (sCsvList)","Test 'CSV to TAB' follows ...")
sCsvList = '111,222,"1a2b, 3ef",123,"dfdfdf", 111, 222,"""Double""Quotes"'

sTabList = udfCsvToTab_1 (sCsvList)
Pause(sCsvList,sTabList)

sTabList = udfCsvToTab_2 (sCsvList)
Pause(sCsvList,sTabList)

sTabList = udfCsvToTab_3 (sCsvList)
Pause(sCsvList,sTabList)

Drop(sCsvList,sTabList)


;..........................................................................................................................................
; --- test ---  udfCsvToTab (sCsvList)
:performancetest
sMsgTitle = "Demo  udfCsvToTab   Performance Test"
sCsvList = '111,222,"1a2b, 3ef",123,"dfdfdf", 111, 222,"""Double""Quotes"'

iTestLoop = 30
iTestMax = 3

For iTest=1 To iTestMax
   Display(1,sMsgTitle,"Running Test %iTest%, please wait ...")
   Exclusive(@ON)
   iStart = GetTickCount()
   For iLoop=1 To iTestLoop
      sResult = udfCsvToTab_%iTest% (sCsvList)
   Next
   iStop = GetTickCount()
   Exclusive(@OFF)
   iTicks%iTest% = iStop-iStart
Next

iTicksMax = 0
For iTest=1 To iTestMax
   iTicksMax = Max(iTicksMax,iTicks%iTest%)
Next
For iTest=1 To iTestMax
   iPct%iTest% = 100*iTicks%iTest%/iTicksMax
Next
sMsgText = ""
For iTest=1 To iTestMax
   sMsgText = StrCat(sMsgText,"Test ",iTest,@TAB,"Ticks = ",@TAB,iTicks%iTest%,@TAB,iPct%iTest%," %%",@CRLF)
Next
Pause(sMsgTitle,sMsgText)

Drop(iLoop,iTest,iTestLoop,iTestMax,iTicksMax,sCsvList,sMsgText,sMsgTitle,sResult,iStart,iStop)
DropWild("iPct*")
DropWild("iTicks*")

:CANCEL
Exit
;==========================================================================================================================================
*EOF*


Seitenanfang/TopOfPage Seitenende/EndOfPage
Seitenanfang/TopOfPage Seitenende/EndOfPage

udfIsStringAtInList (sString, iStart, iLength, sItemList, sDelimiter)

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfisstringatinlist",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfisstringatinlist

#DefineFunction udfIsStringAtInList (sString, iStart, iLength, sItemList, sDelimiter)
If (iStart<1) Then Return (@FALSE)
If (iStart>StrLen(sString)) Then Return (@FALSE)
iCount = ItemCount(sItemList,sDelimiter)
For i=1 To iCount
   If (ItemExtract(i,sItemList,sDelimiter)==StrSub(sString,iStart,iLength)) Then Return (@TRUE)
Next
Return (@FALSE)
;------------------------------------------------------------------------------------------------------------------------------------------
; This Function "udfIsStringAtInList" returns a boolean value
; which indicates if a given SubString matches an Item in a given Itemlist.
;
; Detlev Dalitz.20020719
;------------------------------------------------------------------------------------------------------------------------------------------
#EndFunction

:skip_udfisstringatinlist
;------------------------------------------------------------------------------------------------------------------------------------------


; --- test ---

sString1  = "Hello"
sString2  = "Othello"
iStart   = 4
iLength  = 2
sList    = "hi,lo,up,dn"
sListSep = ","

iIsSubStrInList1 = udfIsStringAtInList(sString1, iStart, iLength, sList, sListSep) ; ==> 1=@TRUE
iIsSubStrInList2 = udfIsStringAtInList(sString2, iStart, iLength, sList, sListSep) ; ==> 0=@FALSE

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
*EOF*


Seitenanfang/TopOfPage Seitenende/EndOfPage
Seitenanfang/TopOfPage Seitenende/EndOfPage

Example for ItemInsert (sItem, iIndex, sList, sDelimiter)

;------------------------------------------------------------------------------------------------------------------------------------------
; Example for ItemInsert (sItem, iIndex, sItemList, sDelimiter)
;------------------------------------------------------------------------------------------------------------------------------------------

; Append at tail.
list  = ""
list  = ItemInsert("A", -1, list, ",")
list  = ItemInsert("B", -1, list, ",")
list  = ItemInsert("C", -1, list, ",")
; ==> list = "A,B,C"

; Insert at head.
list  = ""
list  = ItemInsert("A", 0, list, ",")
list  = ItemInsert("B", 0, list, ",")
list  = ItemInsert("C", 0, list, ",")
; ==> list = "C,B,A"

; Insert after index.
list  = ""
list  = ItemInsert("A", 1, list, ",")
list  = ItemInsert("B", 1, list, ",")
list  = ItemInsert("C", 1, list, ",")
; ==> list = "A,C,B"

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
*EOF*



Page Date
2004-05-18
DD-Software
Kapitel zurück / previous Chapter
Main Index
 
Seite zurück / previous page
Backward
Seite vor / next page
Forward
 
Seitenanfang/TopOfPage
Top
Seitenende/EndOfPage
Bottom
MyWbtHelp current version