Re: [newbie] from right to left
- To: mathgroup at smc.vnet.net
- Subject: [mg48424] Re: [newbie] from right to left
- From: Mark Fisher <mark at markfisher.net>
- Date: Sat, 29 May 2004 03:06:45 -0400 (EDT)
- References: <c96i2u$ika$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Here's one way: str = StringJoin @@ (ToString /@ {a, b, a, j, a, n, o, s, c, a, d, a, b, a, j, a, n, o, s, c, a, d}) First /@ (StringPosition @@ {str, "janos"}) First /@ ((StringLength[str] + 1) - ( StringPosition @@ (StringReverse /@ {str, "janos"}))) --Mark. János wrote: > Hi, > > Let's say I have a list of letter like symbols: > > lst = {a, b, a, j, a, n, o, s, c, a, d,a, b, a, j, a, n, o, s, c, a, d} > > and have a pattern > > pat = {j, a, n, o, s} > > If I want to match by going from left to right > > Position[Partition[lst, Length[pat], 1], pat] > > gives me positions {{4}, {15}}. So far so good. Now the genie came > out from the bottle and reversed the list, so now list reads like: > > revlist = Reverse[lst] > > revlist = {d, a, c, s, o, n, a, j, a, b, a, d, a, c, s, o, n, a, j, a, > b, a} > > and the genie say that now I have to read it from right to left and > find the pattern that way. I could now reverse the pattern > > revpat = Reverse[pat] > > revpat = {s, o, n, a, j} > > and match it up to the reversed list like: > > Position[Partition[revlst, Length[revpat], 1], revpat] > > {{4}, {15}} > > again, but that would not do me any good, because what I want is to > match it from right to left, so the position should be: > > {{8},{19}} > > that is, where "j" is found in revlist by reading from right to left. > Adding Length[pat] to the undesired result of course will give me the > "good" positions, but that looks somewhat "artificial". > > I can program it out procedurally, but that is not "nice" and slow. > > So, in summary, at odd occasions I read lst in western style and I can > find the pattern easily, but at even occasions, when my eyes are > reading the reversed lst from right to left, I have read it in arabic > style and I want to find the pattern that way too. > > Any good hint ? > > Thanks ahead > > János > ------------------------------------------------- > clear perl code is better than unclear awk code; but NOTHING comes > close to unclear perl code > http://www.faqs.org/faqs/computer-lang/awk/faq/ >