Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7896
| From | "Scot T. Martin" <smartin@seas.harvard.edu> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Question about DeleteCases to remove list elements based on first character |
| Date | 2012-01-17 08:32 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jf3bnb$nal$1@smc.vnet.net> (permalink) |
Andrew, there are several ways to get this result, and you were almost there. Here's one example below. The key point is to recognize that you have a list (i.e., not a String action) for which you wish to do a test (i.e., by String actions) on the list elements.
In[1]:= ?Pick
Pick[list,sel] picks out those elements of list for which the corresponding element of sel is True.
In[2]:= ?StringMatchQ
StringMatchQ["string",patt] tests whether "string" matches the string pattern patt.
In[3]:= list = {"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"}
Out[3]= {"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"}
In[4]:= sel = Not[StringMatchQ[#, ";" ~~ __]] & /@ list
Out[4]= {True, False, True, False, False, True}
In[5]:= Pick[list, sel]
Out[5]= {"abc", "ghi", "pqr;X"}
________________________________________
From: Andrew DeYoung [adeyoung@andrew.cmu.edu]
Sent: Monday, January 16, 2012 17:06
Subject: Question about DeleteCases to remove list elements based on first character
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
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 "Scot T. Martin" <smartin@seas.harvard.edu> - 2012-01-17 08:32 +0000
csiph-web