# Replace pattern with the number of matches so far

I want to replace a n-th pattern match in an expression with n. This is a very simple task, though it appears very hard to find an elegant implementation.

For example, with input

inp = {x, y, z, x, y, x, x, z}


I wish to compute, replacing pattern x,

{1, y, z, 2, y, 3, 4, z}


Preferably, I'd like to access the index n in the replacement rule. E.g. something like

Func[inp, x :> Symbol[m<>ToString@#]& ]

>>> {m1, y, z, m2, y, m3, m4, z}


How can I achieve this? It's trivial using Count and a For loop, but is very un-stylistic.

Consider using the Increment operator:

(*In:= *)i = 1;

(*In:= *)inp = {x, y, z, x, y, x, x, z};

(*In:= *)inp /. x :> i++

(*Out= {1, y, z, 2, y, 3, 4, z}*)


Hopefully it's obvious how this can be extended to your example using Symbol.

• If you start with i=0 and then use inp /. x :> ++i, then at the end the index i contains the number of elements matched (instead of one more). Also, if you use inp /. x :> m[++i], then you get an indexing as desired (but without using Symbol, which may not be necessary). – Roman Jan 30 at 21:31
• A pity to have to use a variable, but it's still quite concise – Anti Earth Jan 31 at 16:17
• @Roman quite right, m[ind] is much more elegant, thanks! – Anti Earth Jan 31 at 16:18
• @AntiEarth with Block you can make i a local variable, so you don't need to worry about polluting the name space. – Roman Jan 31 at 16:45