MathGroup Archive 2009

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

Search the Archive

Re: Finding the Position of Elements in a List that

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102230] Re: [mg102157] Finding the Position of Elements in a List that
  • From: Tomas Garza <tgarza10 at msn.com>
  • Date: Sat, 1 Aug 2009 04:05:39 -0400 (EDT)
  • References: <200907310953.FAA19267@smc.vnet.net>

It looks as if StringCases has attribute Listable, although it is not said so.

What you get on applying it to a list is a list of lists, and that is the reason Position will not give you the answer, since it searches for x when you have {x}. A possibility is



In[1]:= a = StringCases[listOfStrings,__~~searchString~~___];



and then



In[2]:= Flatten[Position[listOfStrings,#[[1]]]&/@Select[a,#!={}&]]
Out[2]= {1,3,4}



Cheers,

Tomas



> Date: Fri, 31 Jul 2009 05:53:36 -0400
> From: gregory.lypny at videotron.ca
> Subject: [mg102157] Finding the Position of Elements in a List that Contain a Substring
> To: mathgroup at smc.vnet.net
>
> 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,"}, {"Where 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
>


  • Prev by Date: Re: Help with concatenating a list with &&
  • Next by Date: Re: Creating a Random Function to Select an Irrational
  • 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