Replace a specific element in a list

Hello:

I’ve been searching for the answer to this simple question for over an hour. Its getting late and I am tired.

All that I want to do is to replace a specific element in a text list with a new value. Example:

I have a list: 120, 120, 130, 150, 110

I know the position of the element I want to change: the 2nd element in the list.

Please, how can I use the formula language to change the 2nd element in the list from 120 to 150?

I should add that I’m trying to do this within an @If statement, on the condition that a flag has been set.

@If(flag=“edit”;code to set the 2nd element in the list to 150;“”)

Thank you,

Mark

Subject: Replace a specific element in a list

General code to replace an indexed list element:

originalList := 120:120:130:150:110;

indexToReplace := 2;

newValue := 150;

topList := @Subset(originalList; indexToReplace - 1);

bottomList := @Subset(originalList; -(@Elements(originalList) - indexToReplace);

finalList := topList:newValue:bottomList

Oh, and there’s another way to skip processing that works better for more complex formulas:

@If(flag = “edit”; @Return(“”); “”);

It’s not that putting all of the code in the middle of an @If won’t work, but that the code can get pretty messy. Returning early may make the Pattern People cringe, but in Formula Language, it makes the code a lot easier to write and maintain.

Subject: RE: Replace a specific element in a list

Thank you Stan.

I never thought of tearing the list apart and rebuilding it.

I’ll give it a whirl.

Mark