MathGroup Archive 2011

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

Search the Archive

Re: Using Cases or Position to Find Strings in a List of Strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119536] Re: Using Cases or Position to Find Strings in a List of Strings
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Wed, 8 Jun 2011 07:14:26 -0400 (EDT)

On 6/7/11 at 6:48 AM, gregory.lypny at videotron.ca (Gregory Lypny)
wrote:

>I have a list of strings called myListOfFilePaths and I want to find
>which one of those strings ends in ".DS_Store" (a hidden file
>created by the Mac OS).  I can pick it off using Select as

>Select[myListOfFilePaths, StringMatchQ[#, ___ ~~ ".DS_Store" ~~
>EndOfString] &]

>but I can't do it using Cases or Position

>Cases[myListOfFilePaths, ___ ~~ ".DS_Store" ~~ EndOfString]

Either use StringCases to work with strings or set up a pattern
test for Cases which would be done as:

Cases[myListOfFilePaths,_?(StringMatchQ[#,".DS_Strore]&)]

>Position[myListOfFilePaths, ___ ~~ ".DS_Store" ~~ EndOfString]

You can do the same thing with a pattern test and Position. That is

Position[myListOfFilePaths,_?(StringMatchQ[#,".DS_Strore]&)]

will work. But Mathematica will complain with a beep. So, better
might be

Pick[Range@Length@myListOfFilePaths,
  StringMatchQ[#, ".DS_Store"]&/@myListOfFilePaths]



  • Prev by Date: Re: Displaying Numbers in Non-Scientific Format
  • Next by Date: Re: Using Fit[] inside Plot[] function
  • Previous by thread: Re: Using Cases or Position to Find Strings in a List of Strings
  • Next by thread: Re: Using Cases or Position to Find Strings in a List of Strings