Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7868 > unrolled thread

Question about DeleteCases to remove list elements based on first character

Started byAndrew DeYoung <adeyoung@andrew.cmu.edu>
First post2012-01-16 11:54 +0000
Last post2012-01-17 08:46 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Question about DeleteCases to remove list elements based on first character Andrew DeYoung <adeyoung@andrew.cmu.edu> - 2012-01-16 11:54 +0000
    Re: Question about DeleteCases to remove list elements based on first character Ray Koopman <koopman@sfu.ca> - 2012-01-17 08:41 +0000
    Re: Question about DeleteCases to remove list elements based on first character David Reiss <dbreiss@gmail.com> - 2012-01-17 08:46 +0000

#7868 — Question about DeleteCases to remove list elements based on first character

FromAndrew DeYoung <adeyoung@andrew.cmu.edu>
Date2012-01-16 11:54 +0000
SubjectQuestion about DeleteCases to remove list elements based on first character
Message-ID<jf1362$5hn$1@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

[toc] | [next] | [standalone]


#7918

FromRay Koopman <koopman@sfu.ca>
Date2012-01-17 08:41 +0000
Message-ID<jf3c8h$nh7$1@smc.vnet.net>
In reply to#7868
On Jan 16, 3:54 am, Andrew DeYoung <adeyo...@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

DeleteCases[S, s_/;StringTake[s,1] == ";"]

{"abc", "ghi", "pqr;X"}

[toc] | [prev] | [next] | [standalone]


#7927

FromDavid Reiss <dbreiss@gmail.com>
Date2012-01-17 08:46 +0000
Message-ID<jf3ch5$nl9$1@smc.vnet.net>
In reply to#7868
DeleteCases requires that you work with a pattern. Thus the following
will work for your first example:

DeleteCases[S, _?(StringTake[#, 1] == ";" &)]

gives

{"abc", "ghi", "pqr;X"}

--David

On Jan 16, 6:54 am, Andrew DeYoung <adeyo...@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web