Re: patterns
- To: mathgroup at smc.vnet.net
- Subject: [mg101941] Re: patterns
- From: nma <marxer at mec.li>
- Date: Fri, 24 Jul 2009 06:10:03 -0400 (EDT)
- References: <h41f5m$rin$1@smc.vnet.net> <h46p6l$e7b$1@smc.vnet.net>
On 23 Jul., 09:57, Albert Retey <a... at gmx-topmail.de> wrote: > 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 Hello If you want to exclude something like {c[1],5.6} you can use: Cases[result[[2]], Pattern[dummy, (a | c)[_] -> _]] or short Cases[result[[2]], dummy : ((a | c)[_] -> _)] The syntax of Cases is: Cases[{e1, ...}, pattern] or Cases[{e1, ...}, pattern -> rhs] Therfore you can't use just Cases[result[[2]], (a | c)[_] -> _] because this would fit the "Cases[{e1, ...}, pattern -> rhs]" syntax. Best Regards Norbert Marxer