How to count items and delimiters?
;==========================================================================================================================================
; How to count items and delimiters in a list of strings and extract items from the list?
;
; Examples with ItemCount, ItemExtract, Arrayize, StrScan, udfArrayVBSplit.
;
; (c)Detlev Dalitz.20121027.
;==========================================================================================================================================


GoSub DEFINE_FUNCTIONS
DirChange (DirScript ())


; Example 1. Count items and delimiters in a list using "ItemCount".

strList = "aaa+bbb++ccc+++ddd"
strDelim = "+"

intItems = ItemCount (strList, strDelim)
intDelims = intItems - 1

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 1.' : @LF : strMsgTitle : ' ...' : @LF : '... using "ItemCount".'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'
strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : ItemExtract (intItem, strList, strDelim) : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)



; Example 2. Count items and delimiters in a list using "Arrayize".

strList = "aaa+bbb++ccc+++ddd"
strDelim = "+"
arrList = Arrayize (strList, strDelim)

intItems = ArrInfo (arrList, 1)
intDelims = intItems - 1

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 2.' : @LF : strMsgTitle : ' ...' : @LF : '... using "Arrayize".'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'
strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : arrList[intItem - 1] : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)



; Example 3. Count items and delimiters in a list using "StrScan" (one delimiter).

strList = "aaa+bbb++ccc+++ddd"
strDelim = "+"

intPos = 1
intDelims = 0
While @TRUE
   intPos = StrScan (strList, strDelim, intPos, @FWDSCAN)
   If !intPos Then Break
   intDelims = intDelims + 1
   intPos = intPos + 1
EndWhile
intItems = intDelims + 1

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 3.' : @LF : strMsgTitle : ' ...' : @LF : '... using "StrScan" (one delimiter).'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'
strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : ItemExtract (intItem, strList, strDelim) : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)



; Example 4. Count items and delimiters in a list using "StrScan" (several delimiters).

strList = "aaa+bbb+-ccc+-,ddd"
strDelim = "+-,"

intPos = 1
intDelims = 0
While @TRUE
   intPos = StrScan (strList, strDelim, intPos, @FWDSCAN)
   If !intPos Then Break
   intDelims = intDelims + 1
   intPos = intPos + 1
EndWhile
intItems = intDelims + 1

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 4.' : @LF : strMsgTitle : ' ...' : @LF : '... using "StrScan" (several delimiters).'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'
strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   intPos = StrScan (strList, strDelim, 0, @FWDSCAN)
   strDelimThis = StrSub (strList, intPos, 1)
   strItem = ItemExtract (1, strList, strDelimThis)
   strList = ItemRemove (1, strList, strDelimThis)
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : strItem : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)



; Example 5. Count items and delimiters in a list using "udfArrayVBSplit" (delimiter string).

strList = "aaa+++bbb+++ccc+++ddd"
strDelim = "+++"
arrList = udfArrayVBSplit (strList, strDelim, -1, 0)

intItems = ArrInfo (arrList, 1)
intDelims = intItems - 1

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 5.' : @LF : strMsgTitle : ' ...' : @LF : '... using "udfArrayVBSplit" (delimiter string).'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'
strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : arrList[intItem - 1] : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)


; Example 6. Count items and delimiters in a list using "udfArrayVBSplit" (delimiter string).

strList = "aaa+++bbb+++ccc+++ddd"
strDelim = "++"
arrList = udfArrayVBSplit (strList, strDelim, -1, 0)

intItems = ArrInfo (arrList, 1)
intDelims = intItems - 1

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 6.' : @LF : strMsgTitle : ' ...' : @LF : '... using "udfArrayVBSplit" (delimiter string).'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'
strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : arrList[intItem - 1] : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)



; Example 7. Count items and delimiters in a list using "ItemCount" with surrogate character for delimiter string.

strList = "aaa+++bbb+++ccc+++ddd"
strDelim = "+++"

strMsgTitle = 'Count items and delimiters in a list'
strMsgText = 'Example 7.' : @LF : strMsgTitle : ' ...' : @LF : '... using "ItemCount" with surrogate character for delimiter string.'
strMsgText = strMsgText : @LF : @LF : 'List = "' : strList : '"'
strMsgText = strMsgText : @LF : 'Delimiter = "' : strDelim : '"'

strDelimSurr = Num2Char (7) ; Take a rarely used character.
strList = StrReplace (strList, strDelim, strDelimSurr)
strDelim = strDelimSurr

intItems = ItemCount (strList, strDelim)
intDelims = intItems - 1

strMsgText = strMsgText : @LF : @LF : "Items = " : intItems
strMsgText = strMsgText : @LF : "Delimiters = " : intDelims
strMsgText2 = ""
For intItem = 1 To intItems
   strMsgText2 = strMsgText2 : @LF : 'Item' : intItem : ' = "' : ItemExtract (intItem, strList, strDelim) : '"'
Next
strMsgText = strMsgText : @LF : strMsgText2
Pause (strMsgTitle, strMsgText)


:CANCEL
Exit

;==========================================================================================================================================
:DEFINE_FUNCTIONS
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayVBSplit (strExpression, strDelimiter, intCount, intCompare)
objSC = ObjectCreate ("MSScriptControl.ScriptControl")
objSC.Language = "VBScript"
objSC.AddCode(: "Function F(P1,P2,P3,P4)" : @LF : 'F=Split(P1,P2,P3,P4)' : @LF : "End Function")
Return objSC.Run(: "F", strExpression, strDelimiter, intCount, intCompare)
;..........................................................................................................................................
; This UDF udfArrayVBSplit() returns an one-dimensional array containing a specified number of substrings.
;
; Parameter:
; strExpression
;    String expression containing substrings and delimiters.
;    If expression is a zero-length string, then an empty array will be returned, that is, an array with no elements and no data.
; strDelimiter
;    String used to identify substring limits.
;    If delimiter is a zero-length string, then a single-element array containing the entire expression string is returned.
; intCount
;    Number of substrings to be returned; -1 indicates that all substrings are returned.
; intCompare
;    Numeric value indicating the kind of comparison to use when evaluating substrings.
;       0 ... Perform a binary comparison.
;       1 ... Perform a textual comparison.
;..........................................................................................................................................
; (c)Detlev Dalitz.20110608.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------
Return ; from Gosub DEFINE_FUNCTIONS
;==========================================================================================================================================