Re: Question about DeleteCases to remove list elements based on first character
- To: mathgroup at smc.vnet.net
- Subject: [mg124372] Re: Question about DeleteCases to remove list elements based on first character
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Tue, 17 Jan 2012 05:59:38 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
S = {"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"}; Select[S, StringTake[#, 1] != ";" &] {"abc", "ghi", "pqr;X"} Cases[S, _?(StringTake[#, 1] != ";" &)] {"abc", "ghi", "pqr;X"} DeleteCases[S, _?(StringTake[#, 1] == ";" &)] {"abc", "ghi", "pqr;X"} Pick[S, StringTake[#, 1] != ";" & /@ S] {"abc", "ghi", "pqr;X"} % == %% == %%% == %%%% True Bob Hanlon On Mon, Jan 16, 2012 at 5:06 PM, Andrew DeYoung <adeyoung at andrew.cmu.edu> wrote: > Hi, > > I have a list of strings in which some elements begin with the ";" > character and some do not. The ";" character indicates a program > comment, so I wish to drop all elements in the list that begin with > the ";" character. > > As a simplified example, suppose I have the following list S: > > S = {"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"} > > I wish to drop from S all elements that begin with ";". So, I want to > obtain: > > {"abc", "ghi", "pqr;X"} > > Will you please help me know how to do this? Can this be done with > DeleteCases? For example, I tried: > > DeleteCases[S, StringTake[#, 1] == ";"&] > > and > > DeleteCases[S, ";" ~~ __] > > but I am wrong: these do not work. Can you please help me? > > Thanks so much. > > Andrew DeYoung > Carnegie Mellon University >