# How to use MemberQ for such Variables?

This is NOT the desired out come. I want this test to fail:

pars = Table[ Subscript[\[Tau], ToExpression[ToString[\[Phi]] <> ToString@j]], {j, 5}]
MemberQ[pars, Subscript[\[Tau], p_]]
(* True *)


This is the desired out come. I want this test to pass:

pars2 = Table[ Subscript[\[Tau], ToExpression["p" <> ToString@j]], {j, 5}]
MemberQ[pars2, Subscript[\[Tau], p_]]
(* True *)


Why did the first MemberQ test returns True??

## 1 Answer

In your code p_ simply means a pattern (Blank[]) that you want to name p. Normally this (giving it a name) is done because you want to do something with the matched pattern. The point here is that you are not testing for a subscripted variable that starts with p. You code is essentially equivalent to MemberQ[pars2, Subscript[\[Tau], _]] which is why you get True for both.

If you want to distinguish p from \[Phi] then add a condition to your test:

MemberQ[pars, Subscript[\[Tau], z_ /; StringTake[ToString[z], 1] === "p"]]


Although it is probably even easier just to use strings as subscripts and save your self the trouble of converting back to string for the condition test.