MathGroup Archive 2009

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

Search the Archive

Re: Finding the Position of Elements in a List that Contain a

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102192] Re: Finding the Position of Elements in a List that Contain a
  • From: Armand Tamzarian <mike.honeychurch at gmail.com>
  • Date: Sat, 1 Aug 2009 03:57:55 -0400 (EDT)
  • References: <h4ueun$ipq$1@smc.vnet.net>

On Jul 31, 4:53 am, Gregory Lypny <gregory.ly... at videotron.ca> wrote:
> Hello everyone,
>
> Suppose I have a list of strings, say, sentences such as
>
>         listOfStrings = {"The cat is here.", "It's not here.", =
 "Not in the  
> catalogue,", "Where is the cat?"}
>
> and a string I want to search for
>
>         searchString = "cat"
>
> I can use StringCases to pick off the elements that contain the search =
 
> string
>
>         StringCases[listOfStrings, __ ~~ searchString ~~ ___]
>
>         {{"The cat is here."}, {}, {"Not in the catalogue,"}, {"W=
here is the  
> cat?"}}
>
> But what if I just want to know the positions of the elements that are =
 
> hits?  In this case, it's
>
>         {1, 3, 4}
>
> If I use
>
>         Position[listOfStrings, ___ ~~ theString ~~ ___]
>
> I get
>
>         {}
>
> which is not what I expect.  Also how can I have my searchString  
> treated like it is a word so that 3 is not one of the hits?
>
> Any hints would be much appreciated.
>
>         Gregory

this should work:

Position[listOfStrings, _?(StringMatchQ[#, ___ ~~ "cat" ~~ ___] &)]

Mike


  • Prev by Date: Re: Finding the Position of Elements in a List that
  • Next by Date: Re: Finding the Position of Elements in a List that
  • Previous by thread: Re: Finding the Position of Elements in a List that
  • Next by thread: Re: Finding the Position of Elements in a List that Contain a Substring