MathGroup Archive 2009

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

Search the Archive

Re: How to get position of string in a mixed list? (string pattern

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96211] Re: How to get position of string in a mixed list? (string pattern
  • From: Peter Pein <petsie at dordos.net>
  • Date: Mon, 9 Feb 2009 05:36:13 -0500 (EST)
  • References: <gmndli$s1k$1@smc.vnet.net>

goodhei8 at gmail.com schrieb:
> for list like {"a","abc","dfe","nab",1,2,3,{1,2}}, I want to get all 
> strings' position in the list which match a pattern (like 
> ___~~"ab"~~___). How to do it? How to use a string pattern in a list 
> pattern? how to use a pattern with a condition (both list pattern&/; 
> StringQ[#] and list pattern /;StringQ[]&, no working for me)
> 
> Thanks.
> 

In[1]:=
data={"a","abc","dfe","nab",1,2,3,{1,2,"abba"}};
Position[data,s_String/;StringMatchQ[s,___~~("ab"~~___)]]
Out[2]=
{{2},{4},{8,3}}

the "trick" is to look for strings first (done by the pattern s_String) and to
apply the condition "has 'ab' somwhere in it' later


  • Prev by Date: Map[] and multiple args function
  • Next by Date: Problem importing HTML with Mathematica
  • Previous by thread: Re: How to get position of string in a mixed list? (string pattern
  • Next by thread: Re: How to get position of string in a mixed list? (string pattern