Home Download Buy Blog Forum Support

Find backwards

Find backwards

Postby agibsonsw on Thu Apr 19, 2012 11:45 am

Hello. I have the following Python code to find the previous occurrence of a word:

Code: Select all
new_regions = self.view.find_all(prev_wd, sublime.LITERAL)
new_region = None
for r in reversed(new_regions):
   if r.begin() < prev_pt:
      new_region = r
      break
if new_region:

where prev_pt is the begin-point of the word I'm looking for (prev_wd).

Well, it works, but I'm assuming there is a neater, more efficient, way of doing this?

An alternative, but messier approach, might be to keep subtracting some number (10) for a value x, using find() forward from this x-point. But perhaps a single call to find_all will always be more efficient than repeated calls to find? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Find backwards

Postby agibsonsw on Thu Apr 19, 2012 12:04 pm

No worries ;) 8-)

Code: Select all
new_regions = (r for r in reversed(self.view.find_all(prev_wd, sublime.LITERAL)) if r.begin() < prev_pt)
try:
   new_region = new_regions.next()
except StopIteration:
   new_region = None
if new_region:


Added: A beautiful piece of code.. if I say so myself :lol:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm


Return to Technical Support

Who is online

Users browsing this forum: Alexa [Bot] and 19 guests