Re: Position
- To: mathgroup at smc.vnet.net
- Subject: [mg67668] Re: Position
- From: bghiggins at ucdavis.edu
- Date: Tue, 4 Jul 2006 01:57:02 -0400 (EDT)
- References: <e8ashi$g8j$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Bruce, Consider the following list: str = {"The" , " negative", "", "signed", "abc elements", " go", "abc to" , "zero" } Now let us devise a test for each element of our list using StringMatchQ myTest = StringMatchQ[ToString[#], "abc" ~~ __] & Then apply the test to your pattern in Position Position[str,_?myTest] {{5},{7}} Note the reason we wrap the slot value of our test with ToString is that the algorithm of Position inserts the list str into the slot and since it has a head of List you get an error message, though the test does work. Use Trace to see how this occurs. Hope this helps, Cheers, Brian Bruce Colletti wrote: > Let X be a list of strings having diverse lengths (some are the null string ""). > > What command returns the position in X of the first element that starts with "abcd"? > > Presumably the command looks like Position[X,...,Heads->False], but my attempts have come up dry. Although StringTake[Y,4]=="abcd" would seem to be part of the command, this fails when the string's length is less than 4. > > Thankx. > > Bruce