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??


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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.