1
$\begingroup$

I would like to have a function that can extract from an expression a list of values that are within the brackets of a certain header at any level:

HeaderContent[Expression_, Header_]

For example consider expr=a[b[q],c[{b[w],m[{d,f,g}],y[s]}],d[u],m[p]];, so that

HeaderContent[expr,m]

{{d,f,g},p}

Does this function exist? If not, how could one implement it?

$\endgroup$
  • 2
    $\begingroup$ Extract[expr, Position[expr, m[___]]] /. m[a___] :> a ? $\endgroup$ – Dr. belisarius Aug 31 '15 at 22:08
  • 2
    $\begingroup$ Cases[expr, m[a___] :> a, Infinity]? $\endgroup$ – Dr. belisarius Aug 31 '15 at 22:09
  • $\begingroup$ Works like a charm, thank you! $\endgroup$ – Kagaratsch Aug 31 '15 at 22:15
2
$\begingroup$
Cases[expr, m[a___] :> a, Infinity]
(* {{d,f,g},p}*)

Be careful with

expr = m@m@p;
Cases[expr, m[a___] :> a, Infinity]
(* {p} *)

versus

expr = m@m@p;
Cases[expr, m[a___] :> a, {0, Infinity}]
(* {p, m[p]} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.