Re: Testing the Head of List Elements
- To: mathgroup at smc.vnet.net
- Subject: [mg25236] Re: [mg25200] Testing the Head of List Elements
- From: BobHanlon at aol.com
- Date: Sun, 17 Sep 2000 04:47:14 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 9/15/2000 2:47:58 AM, mgb_news at hotmail.com writes: >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.). > Here are some examples testList = {1, 2, "three", 4, "five", x, y, Pi, 3 + 2I}; Cases[testList, _String] Cases[testList, _Symbol] Cases[testList, _Symbol?(! NumericQ[#] &)] Cases[testList, _Symbol?NumericQ] Cases[testList, _?NumberQ] Cases[testList, _?NumericQ] Cases[testList, _Complex] Select[testList, Im[#] == 0 &] Cases[testList, _Integer] Select[testList, Element[#, Reals] &] There is an equivalent Select statement for every Cases statement and vice versa. Bob Hanlon