Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7928
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Question about DeleteCases to remove list elements based on first character |
| Date | 2012-01-17 08:46 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jf3ci3$nln$1@smc.vnet.net> (permalink) |
On 1/16/12 at 5:06 PM, adeyoung@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}
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Question about DeleteCases to remove list elements based on first character Bill Rowe <readnews@sbcglobal.net> - 2012-01-17 08:46 +0000
csiph-web