I've been using this post (how to do replacing-item in use nested list) as a guide to work out how to replace items in a list, that meet a given criteria.
Specifically, I want to replace all zeros in a list with the value = 0.5. However, the code I've come up with only seems to replace the first zero in the list and I can't seem to work out why.
This is my code:
to-report A-new-list-without-zeros [old new the-list]
let A-index-list n-values length the-list [?]
( foreach A-index-list the-list
[ if ?2 = old
[ report replace-item ?1 the-list new ]
])
report the-list
end
And this is what happens:
observer> show A-new-list-without-zeros 0 0.5 [0 1 0 5 5 0]
observer: [0.5 1 0 5 5 0]
Any help would be much appreciated! Thanks