MathGroup Archive 2000

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

Search the Archive

Re: Testing the Head of List Elements

  • To: mathgroup at
  • Subject: [mg25233] Re: [mg25200] Testing the Head of List Elements
  • From: Ken Levasseur <Kenneth_Levasseur at>
  • Date: Sun, 17 Sep 2000 04:47:12 -0400 (EDT)
  • Organization: UMass Lowell
  • References: <>
  • Sender: owner-wri-mathgroup at


It's clear exactly what form you want the input to take, so you probably
will need to tailor this to your needs.   HeadSort produces a list of
items of the form {"ahead",list of expressions in data with head "ahead"}

This what you had in mind?

I'm sure the code could be improved on but I've got bring my son to
school now.

Ken Levasseur
UMass Lowell

test = {{2}, "2", 2, two, Hold[1 + 1], {two}}
{{2}, "2", 2, two, Hold[1 + 1], {two}}
HeadSort[data_List] := 
  Module[{t, types = Union[Head /@ data]}, 
    Map[(t = #; {t, Select[data, (Head[#] == t) &]}) &, types]]
{{Hold, {Hold[
        1 + 1]}}, {Integer, {2}}, {List, {{2}, {two}}}, {String, {"2"}}, \
{Symbol, {two}}}

Marshall Bartlett wrote:
> I'm something of a newbie with Mathematica; please excuse my ignorance.
> I am looking for a clever way to select elements from a list based on their
> type (Head).  Specifically, I want to extract all the elements of type
> dtring from a list of elements of different types (string, number,
> character, etc.).
> Any suggestions?
> --
> --------------------------------------------------------------
> Marshall Bartlett
> Department of Geology and Geophysics
> University of Utah
> mgb_news at
> --------------------------------------------------------------

  • Prev by Date: RE: ParametricPlot3D is buggy
  • Next by Date: RE: How to display the desired numerical precision?
  • Previous by thread: Testing the Head of List Elements
  • Next by thread: Re: Testing the Head of List Elements