# Finding rules with Cases

**URL:** <https://community.wolfram.com/t/finding-rules-with-cases/133>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [January 10, 2013, 8:54pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133 "2013-01-10T20:54:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![elizabeths](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/elizabeths/32/10570_2.png) [@elizabeths](https://community.wolfram.com/u/elizabeths)\
**Post date:** [January 10, 2013, 8:54pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/1 "2013-01-10T20:54:31Z")

</div>

I’m trying to process a list of rules. I’m trying to find specific rules inside of Cases, but it keeps turning everything into the value I’m trying to find. I want it to return the whole rule. For example:

```auto
set1 = {"tree1" -> "green", "tree2" -> "red", "tree3" -> "green", 
   "tree4" -> "orange", "tree5" -> "orange", "tree6" -> "red", 
   "tree7" -> "green", "tree8" -> "red", "tree9" -> "orange", 
   "tree10" -> "dead"};
Cases[set1, _ -> "green"]

```

Any advice?

---

<div class="post-metadata">

**Author:** ![carlw](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/carlw/32/20872_2.png) [@carlw](https://community.wolfram.com/u/carlw)\
**Post date:** [January 11, 2013, 4:33pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/2 "2013-01-11T16:33:15Z")

</div>

Cases with a Rule for a second argument has a special meaning, so you need to tweak your usage a bit. One possibility is:

```auto
Cases[set1, HoldPattern[_ -> "green"]]

```

---

<div class="post-metadata">

**Author:** ![nickl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/nickl/32/21430_2.png) [@nickl](https://community.wolfram.com/u/nickl)\
**Post date:** [January 11, 2013, 4:34pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/3 "2013-01-11T16:34:17Z")

</div>

This should do it:

```auto
Cases[set1, HoldPattern[_ -> "green"]]
{"tree1" -> "green", "tree3" -> "green", "tree7" -> "green"}

```

---

<div class="post-metadata">

**Author:** ![paritoshm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/paritoshm/32/12595_2.png) [@paritoshm](https://community.wolfram.com/u/paritoshm)\
**Post date:** [January 11, 2013, 4:37pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/4 "2013-01-11T16:37:32Z")

</div>

One approach could be to do the following:

```auto
In[19]:= Cases[set1, (x_Rule /; Last[x] === "green")]
Out[19]= {"tree1" -> "green", "tree3" -> "green", "tree7" -> "green"}

```

---

<div class="post-metadata">

**Author:** ![joshl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/joshl/32/14788_2.png) [@joshl](https://community.wolfram.com/u/joshl)\
**Post date:** [January 11, 2013, 4:38pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/5 "2013-01-11T16:38:25Z")

</div>

Since Cases allows one to replace a pattern via this form:

```auto
Cases[{e1, ...}, pattern->rhs]

```

You could wrap the pattern in HoldPattern so it knows that you aren’t trying to do a recplacement:

```auto
In[20]:= Cases[set1, HoldPattern[_ -> "green"]]
Out[20]= {"tree1" -> "green", "tree3" -> "green", "tree7" -> "green"}

```

---

<div class="post-metadata">

**Author:** ![arnoudb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arnoudb/32/41177_2.png) [@arnoudb](https://community.wolfram.com/u/arnoudb)\
**Post date:** [January 11, 2013, 4:39pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/6 "2013-01-11T16:39:36Z")

</div>

Using Select[] is an alternative sometimes:

```auto
In[1]:= set1 = {"tree1" -> "green", "tree2" -> "red",
"tree3" -> "green", "tree4" -> "orange", "tree5" -> "orange",
"tree6" -> "red", "tree7" -> "green", "tree8" -> "red",
"tree9" -> "orange", "tree10" -> "dead"};

In[2]:= Select[set1, Last[#] == "green" &]

Out[2]= {"tree1" -> "green", "tree3" -> "green", "tree7" -> "green"}

```

---

<div class="post-metadata">

**Author:** ![chetiyas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chetiyas/32/38741_2.png) [@chetiyas](https://community.wolfram.com/u/chetiyas)\
**Post date:** [January 11, 2013, 5:06pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/7 "2013-01-11T17:06:20Z")

</div>

```auto
Cases[set1, _ -> "green"]

```

tells Cases to replace expressions matching

```auto
_

```

(any Mathematica expression) with “green”. That is the way Cases works. But what you want is to find cases of occurrence of the pattern

```auto
(_ -> "green")

```

To do this you must use

```auto
HoldPattern[_ -> "green"]

```

which is the same as

```auto
_ -> "green"

```

for pattern matching. There is an example in the “Possible issues” section in the documentation of Cases.

So we get

```auto
In[53]:= Cases[set1, HoldPattern[_ -> "green"]]

Out[53]= {"tree1" -> "green", "tree3" -> "green", "tree7" -> "green"}

```

---

<div class="post-metadata">

**Author:** ![aarone](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/aarone/32/37154_2.png) [@aarone](https://community.wolfram.com/u/aarone)\
**Post date:** [January 11, 2013, 5:06pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/8 "2013-01-11T17:06:58Z")

</div>

Here is another approach:

```auto
In[1]:= rules = {"tree1" -> "green", "tree2" -> "red",
"tree3" -> "green", "tree4" -> "orange", "tree5" -> "orange",
"tree6" -> "red", "tree7" -> "green", "tree8" -> "red",
"tree9" -> "orange", "tree10" -> "dead"};

In[2]:= Cases[rules, p:(_ -> "red") :> p]
Out[2]= {"tree2" -> "red", "tree6" -> "red", "tree8" -> "red"}

```

---

<div class="post-metadata">

**Author:** ![jeffb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jeffb/32/36128_2.png) [@jeffb](https://community.wolfram.com/u/jeffb)\
**Post date:** [January 11, 2013, 5:10pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/9 "2013-01-11T17:10:37Z")

</div>

I think you want this:

```auto
In[14]:= Cases[set1, HoldPattern[_ -> "green"]]

Out[14]= {"tree1" -> "green", "tree3" -> "green", "tree7" -> "green"}

```

-Jeff

---

<div class="post-metadata">

**Author:** ![cfrench](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/cfrench/32/27859_2.png) [@cfrench](https://community.wolfram.com/u/cfrench)\
**Post date:** [January 11, 2013, 8:58pm UTC](https://community.wolfram.com/t/finding-rules-with-cases/133/10 "2013-01-11T20:58:32Z")

</div>

FilterRules was made for this type of thing, but it is keyed off matches on the left hand side

```auto
Reverse/@FilterRules[Reverse/@set1, "green"]

```
