udfItemListCsvReplaceV3
str udfItemListCsvReplaceV3 (str, int, str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListCsvReplaceV3 (strItem, intIndex, strList, strDelimiter)
If strList == "" Then Return strList
strFileTemp = FileCreateTemp ("WB")
If !FilePut (strFileTemp, strList) Then ErrorEvent (-1, 7001, "udfItemListCsvReplace: Cannot create temp file (text): " : strFileTemp) ; Throw Minor error.
arrCsv = ArrayFileGetCSV (strFileTemp, 1)
intArrElements = ArrInfo (arrCsv, 2)
If intIndex > intArrElements Then intIndex = intArrElements
If intIndex < 0 Then intIndex = intArrElements
arrCsv[0, intIndex - 1] = strItem
If !ArrayFilePutCSV (strFileTemp, arrCsv) Then ErrorEvent (-1, 7002, "udfItemListCsvReplace: Cannot create temp file (csv): " : strFileTemp) ; Throw Minor error.
strList = ItemRemove (-1, FileGet (strFileTemp), @CR) ; Remove trailing @CRLF.
If !FileDelete (strFileTemp) Then ErrorEvent (-1, 7003, "udfItemListCsvReplace: Cannot delete temp file (csv): " : strFileTemp) ; Throw Minor error.
Return strList
;..........................................................................................................................................
; This UDF "udfItemListCsvReplaceV3" 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:
; A null list ("") is not a valid list.
;
; (c)Detlev Dalitz.20120108.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

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

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

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


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

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


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

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

:CANCEL
Exit