This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Cases

Cases[{e1, e2, ...}, pattern]
gives a list of the ei that match the pattern.
Cases[{e1, ...}, pattern->rhs]
gives a list of the values of rhs corresponding to the ei that match the pattern.
Cases[expr, pattern, levelspec]
gives a list of all parts of expr on levels specified by levelspec which match the pattern.
Cases[expr, pattern->rhs, levelspec]
gives the values of rhs which match the pattern.
Cases[expr, pattern, levelspec, n]
gives the first n parts in expr which match the pattern.
  • The first argument to Cases need not have head List.
  • Cases[expr, pattern:>rhs] evaluates rhs only when the pattern is found.
  • Cases uses standard level specifications:
nlevels 1 through n
Infinitylevels 1 through Infinity
{n}level n only
{n1,n2}levels n1 through n2
  • The default value for levelspec in Cases is {1}.
  • A positive level n consists of all parts of expr specified by n indices.
  • A negative level -n consists of all parts of expr with depth n.
  • Level -1 consists of numbers, symbols and other objects that do not have subparts.
  • Level 0 corresponds to the whole expression.
  • With the option setting Heads->True, Cases looks at heads of expressions, and their parts.
  • Cases traverses the parts of expr in a depth-first order, with leaves visited before roots.
New in 1