Re: patterns
- To: mathgroup at smc.vnet.net
- Subject: [mg101937] Re: patterns
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Thu, 23 Jul 2009 03:57:37 -0400 (EDT)
- References: <h41f5m$rin$1@smc.vnet.net> <h46p6l$e7b$1@smc.vnet.net>
Hi,
> This works fine without Verbatim or HoldPattern:
>
> In[1]:= result = {11.2,
> {dUp[1]->0., dUp[2]->0., dUp[3]->0., dUp[4]->2.1,dUp[5]->0.,
> dUm[1]->0., dUm[2]->2.6,dUm[3]->0.5,dUm[4]->0., dUm[5]->0.,
> dLp[1]->0., dLp[2]->0., dLp[3]->0., dLp[4]->1.4,dLp[5]->0.,
> dLm[1]->0., dLm[2]->3.1,dLm[3]->1.5,dLm[4]->0., dLm[5]->0.,
> a[1]->6.75, a[2]->1.25, c[1]->1.65, c[2]->0.15}};
>
> In[2]:= Cases[result[[2]], Rule_[(a|c)[_],_] ]
>
> Out[2]= {a[1]->6.75, a[2]->1.25, c[1]->1.65, c[2]->0.15}
>
and this does, too:
Cases[result[[2]], _[(a | c)[_], _]]
note that the way you used Rule, you are creating a named part of the
pattern (whose name is Rule) that you don't ever use. So its name does
not matter and no harm is done by using Rule as that name. If you'd use
the name Rule in a possible RHS of the rule, you might create surprising
results, though...
The difference to other solutions is that it will also match different
occurrences of a or c, not only rules, e.g. something like {c[1],5.6}
which may or may not be intended...
hth,
albert