udfItemListCsvReplaceV1
str udfItemListCsvReplaceV1 (str, int, str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListCsvReplaceV1 (strItem, intIndex, strList, strDelimiter)
If strList == "" Then Return strList
strList = udfItemListCsvToTab (strList, strDelimiter, @TRUE)
strList = ItemReplace (strItem, intIndex, strList, @TAB)
strList = StrReplace (strList, @TAB, strDelimiter)
Return strList
;..........................................................................................................................................
; This UDF "udfItemListCsvReplaceV1" replaces an item in a list with a new item.
; It returns a new list, with the specified replacement made; the original list is unchanged.
; For example, specifying an index of 1 causes the first item in the list to be replaced with the new item.
; Using -1 as the index will replace the last item in the list.
;
; Note:
; The original list must have no embedded @TAB character.
; A null list ("") is not a valid list.
;
; (c)Detlev Dalitz.20120108.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListCsvToTab (strCsvList, strDelimiter, blnSaveQuotes)
If strDelimiter == "" Then strDelimiter == "," ; strDelimiter = ";"  European semicolon separated values.
strSurrogate = Num2Char (7) ; Placeholder.
strQuote = '"'
strQuote2 = StrFill (strQuote, 2)
strTabList = ""
strCsvList = StrReplace (strCsvList, strQuote2, strSurrogate) ; Substitute following double Quotes with placeholder.
If ItemCount (strCsvList, strQuote) == 1
   strTabList = StrReplace (strCsvList, strDelimiter, @TAB)
Else
   While strCsvList != ""
      strItem = ItemExtract (1, strCsvList, strQuote)
      strItem = StrReplace (strItem, strDelimiter, @TAB)
      strTabList = StrCat (strTabList, strItem)
      strItem = ItemExtract (2, strCsvList, strQuote)
      If strItem != ""
         If blnSaveQuotes Then strItem = StrCat (strQuote, strItem, strQuote) ; Enclose text items with quotation marks.
         strTabList = StrCat (strTabList, strItem)
      EndIf
      strCsvList = ItemRemove (1, strCsvList, strQuote)
      strCsvList = ItemRemove (1, strCsvList, strQuote)
   EndWhile
EndIf
If blnSaveQuotes Then strQuote = strQuote2
strTabList = StrReplace (strTabList, strSurrogate, strQuote) ; Replace placeholder with one quotation mark.
Return strTabList
;..........................................................................................................................................
; This UDF udfItemListCsvToTab is able to handle strings like
; strCsvList = '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
; strTabList='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
; Slightly modified by Detlev Dalitz.20090604.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strCsvLineIn = `1,"lastname,firstname",3,4,,"some other words",7`

; Test 1.
strCsvLineOut = udfItemListCsvReplaceV1 (`"test item 3"`, 3, strCsvLineIn, ",") ; Change item at position 3.

strMsgTitle = "udfItemListCsvReplaceV1|Test.1|Replace item 3"
strMsgText = "strCsvLineIn = " : @LF : strCsvLineIn : @LF : @LF : "strCsvLineOut = " : @LF : strCsvLineOut
Pause (strMsgTitle, strMsgText)


; Test 2.
strCsvLineOut = udfItemListCsvReplaceV1 (`"test last item"`, -1, strCsvLineIn, ",") ; Change item at last position.

strMsgTitle = "udfItemListCsvReplaceV1|Test.2|Replace last item"
strMsgText = "strCsvLineIn = " : @LF : strCsvLineIn : @LF : @LF : "strCsvLineOut = " : @LF : strCsvLineOut
Pause (strMsgTitle, strMsgText)


; Test 3.
strCsvLineIn = ""
strCsvLineOut = udfItemListCsvReplaceV1 (`"test last item"`, -1, strCsvLineIn, ",") ; Change item at last position.

strMsgTitle = "udfItemListCsvReplaceV1|Test.3|Null list"
strMsgText = "strCsvLineIn = " : @LF : strCsvLineIn : @LF : @LF : "strCsvLineOut = " : @LF : strCsvLineOut
Pause (strMsgTitle, strMsgText)

:CANCEL
Exit