Re: Question about DeleteCases to remove list elements based on first character
- To: mathgroup at smc.vnet.net
- Subject: [mg124351] Re: Question about DeleteCases to remove list elements based on first character
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Tue, 17 Jan 2012 03:30:45 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 1/16/12 at 5:06 PM, adeyoung at andrew.cmu.edu (Andrew DeYoung) wrote: >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] == ";"&] This is very close to something that works. Doing: In[2]:= DeleteCases[{"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"}, _?(StringTake[#, 1] == ";" &)] Out[2]= {abc,ghi,pqr;X} gives the desired result. But given comparison is between strings, it might be better to code this as: In[3]:= DeleteCases[{"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"}, _?(StringMatchQ[#, ";" ~~ __] &)] Out[3]= {abc,ghi,pqr;X}