# Replacing a specific pattern of values at level 2 in a list

I've a list of lists,

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}}


and each nested list has the same length, What is the proper way to replace the occurances of all m* values, i.e., m12, m22, etc., with a particular value say x.

All I want is this result

tl = {{True, False, x}, {False, x, x}, {x, False, True}}


Any help will be appreciated.

• Is there anything other than True, False and m*?
– Kuba
Commented Jun 7, 2014 at 19:23
• No there are only True, False or m*
– aark
Commented Jun 7, 2014 at 19:30

One rather literal (string-based) interpretation of your question:

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};

tl /. s_ /; StringTake[ToString[s], 1] == "m" :> X


{{True, False, X}, {False, X, X}, {X, False, True}}

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};

Replace[tl, Except[True | False] :> z, {2}]

(* {{True, False, z}, {False, z, z}, {z, False, True}} *)


In this way the True and False conditions are analyzed

tl = {{True, False, m12}, {False, m22, m33}, {m32, False, True}};
Map[If[ToString[#1] == "True" || ToString[#1] == "False", #1,
x] & , tl, {2}]
`

$\left( \begin{array}{ccc} \text{True} & \text{False} & x \\ \text{False} & x & x \\ x & \text{False} & \text{True} \\ \end{array} \right)$