Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Cases vs. StringCases vs. Select and StringMatchQ vs. StringFreeQ
Replies: 1   Last Post: Jul 23, 2010 7:11 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
David Latin

Posts: 7
Registered: 6/11/10
Cases vs. StringCases vs. Select and StringMatchQ vs. StringFreeQ
Posted: Jul 22, 2010 5:42 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello,
I am currently working on manipulating data in "vCard"-like format, and have
become confused by the actions of the Cases, StringCases and Select
functions.
Consider the small list:

In[1]:= list = {"DTEND:19260412T175900", "DTEND:20070207T050000",
"END:VCALENDAR", "MM"} ;

In[2]:= Cases[list, ___~~"END:"~~___]
Out[2]= {}
So pattern-matching obviously does not work with Cases for a list of
strings.

The documentation for Cases does not refer to patterns in strings, so I
tried

In[3]:= StringCases[list, ___~~"END:"~~___]
Out[3]=
{{"DTEND:19260412T175900"},{"DTEND:20070207T050000"},{"END:VCALENDAR"},{}}
The problem here is that empty elements can be returned.

So next I tried

In[4]:= Select[list, ___~~"END:"~~___]
Out[4]= {}
Obviously not working.

Next I tried

In[5]:= Select[ list, StringMatchQ[#, "*END:*"] & ]
Out[5]= {"DTEND:19260412T175900", "DTEND:20070207T050000", "END:VCALENDAR"}

This is fine.
But what if I only want the "END:" lines and not the "DTEND:" lines ?

It may be appropriate to make use of

In[6]:= Select[ list, StringFreeQ[#, "*DTEND:*"] & ]
Out[6]= {"DTEND:19260412T175900", "DTEND:20070207T050000", "END:VCALENDAR",
"MM"}
Not as expected!

But, in the end, what works is:

In[7]:= Select[ list, StringMatchQ[#, "*END:*"] && ! StringMatchQ[#,
"*DTEND:*"] & ]
Out[7]= {"END:VCALENDAR"}

I know I could have used "END*" instead of "*END*", but that's not the point
here.

My questions then are:
Why doesn't Cases work for a list of strings ?
Why doesn't Select work for patterns with the ~~ operator ?
Why doesn't StringFreeQ act in the same way as !StringMatchQ ?

Any help over this confusion would be very much appreciated!
Thank you,
David



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.