MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Question about DeleteCases to remove list elements based on first character

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124349] Re: Question about DeleteCases to remove list elements based on first character
  • From: Tomas Garza <tgarza10 at msn.com>
  • Date: Tue, 17 Jan 2012 03:30:03 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

I think perhaps the easiest approach would be
In[2]:= S = {"abc", ";def", "ghi", ";jkl", "; mno", "pqr;X"};
In[3]:= Select[S, StringTake[#, 1] != ";" &]
Out[3]= {"abc", "ghi", "pqr;X"}
or, of course,
In[5]:= DeleteCases[S, x_ /; StringTake[x, 1] == ";"]
Out[5]= {"abc", "ghi", "pqr;X"}
(by the way, avoid capitalizing the initial letter for your variable names, to avoid conflicts with Mathematica's own symbols).
-Tomas

> Date: Mon, 16 Jan 2012 17:06:38 -0500
> From: adeyoung at andrew.cmu.edu
> Subject: Question about DeleteCases to remove list elements based on first character
> To: mathgroup at smc.vnet.net
>
> 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


  • Prev by Date: Re: remote kernel problem
  • Next by Date: Re: Nested Manipulate copies to clipboard as "grey box"
  • Previous by thread: Re: Question about DeleteCases to remove list elements based on first character
  • Next by thread: Re: Question about DeleteCases to remove list elements based on first character