I have a list of text and need to replace an element based on the text that appears in the element before it. Example of my list:
list={{"yes","can","fgh"},{"yes","can",""},{"yes","not","fgh"},{"yes","can","srts"},
{"yes","not","h"}}
I would like to replace all items in column 3 with "This" when column 2's entry is "can".
I have tried to do this using /. and If but without success. I think I need to use a rule but don't know the first thing about how to do this.
Replace, Mathematica tries to match the first argument using rules provided in the second argument. In this case the first argument is your originallistwhich containes a list of triples. So I matched those triples with the rule{x_, "can", _}. What this rule says is match any triple where the second column is "can", then using:>(RuleDelayed), I then replace the matched triples with{x, "can", "This"}, where I keep the first two columns the same but replace the third column with the desired"This"– RunnyKine Oct 13 '14 at 02:13ss = Alternatives["can", "cann", ...], then usessin the code above. – RunnyKine Oct 13 '14 at 02:59sslike this:Replace[list, {x_, y : ss, _} :> {x, y, "This"}, 1]– RunnyKine Oct 13 '14 at 04:00