re: pattern matching in M
- To: mathgroup at yoda.physics.unc.edu
- Subject: re: pattern matching in M
- From: gaylord at ux1.cso.uiuc.edu
- Date: Mon, 18 May 1992 16:24:08 -0500
somebody wrote: given x = {{3,4},{2,3},{1,2},{5,6}} he or she wants to pick out the pairs whose second elements are either 2 or 6 and add the pairs together, preferably using a transformation rule. here you go: Cases[x, {_,2}|{_,6}] {{1, 2}, {5, 6}} Cases[x, {_,2}|{_,6}]/.List->Plus 14 this doesn't work because it replaces every occurance of List Apply[Plus,Cases[x, {_,2}|{_,6}]] {6, 8} this works fine since it only replaces the head of the entire expression Cases[x, {_,2}|{_,6}]/.{x_, y_} :> x + y {6, 8} this also works fine