WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: JTaylor on September 02, 2025, 07:19:14 AM

Title: ItemReplace
Post by: JTaylor on September 02, 2025, 07:19:14 AM
Perhaps this has come up before but thought I would mention it, as it seems to be inconsistent behavior compared to some other functions.  Given the following, searchinfo ends up being a blank string.  While I can see the argument for that being the case, it seems like the value should end up being "HELLO".   If you do a similar thing with ItemInsert, it has an ending value of "HELLO" rather than being a blank string.

Jim

cu = "HELLO"
searchinfo = ""
searchinfo = ItemReplace(cu, 1, searchinfo, "#")
Message("SI",searchinfo)
Title: Re: ItemReplace
Post by: kdmoyers on September 02, 2025, 09:12:05 AM
I've stared at this for 5 minutes, and I think I agree with you.

Maybe there might be some code out there that depends on this behavior? Its an edge case, the empty list, so... tough call.

Title: Re: ItemReplace
Post by: bottomleypotts on September 02, 2025, 11:13:29 AM
So:

count=ItemCount(searchinfo, "#")

should return 1 (a blank string) but instead returns 0 which is why it cannot replace.

Likewise:

extract1=ItemExtract(1, searchinfo, "#")

should create an error, rather than extract1 being blank ("").
Title: Re: ItemReplace
Post by: spl on September 02, 2025, 02:15:18 PM
Perhaps documentation should address using "" as a delimiter, as some might think that flies in the face of a concept of a 'list'. This, however, does not remediate the previous posts which are valid.
Title: Re: ItemReplace
Post by: chrislegarth on September 04, 2025, 06:36:44 PM
I think ItemReplace works as I would expect.  With ItemReplace you are specifying which item (index) to replace in the list.
An empty string list does not have an item 1 to replace so the empty string is maintained.  Changing List to equal "#" makes it have two empty strings to work with and a replacement occurs.

With the first code example, you would either do List = "HELLO" or List = Replacement

This code made more sense to me...

No replacement occurs
Replacement = "HELLO"
Index = 1
List = ""
List = ItemReplace(Replacement, Index, List, "#")
Message("Replacement",List)

Replacement occurs
Replacement = "HELLO"
Index = 1
List = "#"
List = ItemReplace(Replacement, Index, List, "#")
Message("Replacement",List)
Title: Re: ItemReplace
Post by: bottomleypotts on September 04, 2025, 10:40:44 PM
If you can replace a blank item because there are many, then you should be replace a blank item because there is 1.