According to the text in the NetLogo dictionary “”replace-item” is used in conjunction with “set” to change a list”. But how is it done? And how does it work with list of lists?
- Answer to the first question (how to use the primitive replace-item to change a list):
- Let’s say that you have a list [1 2 3 4 5] called myList and you want to replace the first item by the string “a”. Check the picture below for an example using the command center:
- So, it is only “re-setting” the list replacing the desired item by the desired value.
- Now the answer to the second question (how to use the primitive replace-item to change an item in a list that is inside another list – that’s in a “list of list”):
- If you try to do it directly using the same procedure presented above, the result will be that the original list will be truncated. All other sublists will be lost and the only remaining is the one whose item was modified. Please check the pic below:
- As you could see, the original list was lost. Only remaining the sublist with the item changed. This happens, because the sintax of the primitive replace-item considers the second argument (replace-item index list value) as one single simple list.
- To solve this issue, that’s to have the item of the list inside a list modified and preserve the original list, you have to use an auxiliar list. Check the pic below:
- So, I have used the auxiliar list auxList to receive the value of the internal list I wanted to modify, and then I replaced the whole item of the original list myList using the sintax presented before (treating it as a simple list then).
I hope it helps. It helped me a lot. 🙂