views:

145

answers:

1

Example:

test = {"a" -> {{1}, 12}, "b" -> {13}}

I'd like to find all expressions in the list with this pattern:

_ -> {_,_}

The first element, "a" -> {{1}, 12}, is represented by this pattern.

However, none of these expressions work:

Cases[test,_->{_,_}], Cases[test,_->_], Cases[test,Rule[_,_]], etc.

Any advice would be appreciated.

+5  A: 

What you're looking for is HoldPattern:

In[1]:= test = {"a" -> {{1}, 12}, "b" -> {13}};

In[2]:= Cases[test, HoldPattern[_ - >{_, _}]] // InputForm
Out[2]= {"a" -> {{1}, 12}}

EDIT: This also works with named pattern variables.

In[3]:= Cases[test, HoldPattern[_ -> {x_, _}] :> x]
Out[3]= {{1}}
Pillsy
Excellent. This seems to be what I'm looking for. Is it possible to have named pattern variables, e.g. _->{_,_x} /; x<10while using HoldPattern?
Tony
Excellent! Top dawg.
Tony