Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1992

[Date Index] [Thread Index] [Author Index]

Search the Archive

re: pattern matching in M

  • To: mathgroup at
  • Subject: re: pattern matching in M
  • From: gaylord at
  • Date: Mon, 18 May 1992 16:24:08 -0500

somebody wrote:

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
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

  • Prev by Date: Plotting (x,y,z)
  • Next by Date: Hardware Mma ?
  • Previous by thread: Plotting (x,y,z)
  • Next by thread: Hardware Mma ?