Thanks for all the interesting replies to this query. I used "offset" rather than (what most replies suggested) "repeat" with (or for) each character thinking it would be faster, but that seems to be true only if the string is extremely long and the sought for character very far from the front. And I tried to match the first "(" rather than building a list of all matches. There is no practical need for yet another approach, but in case others will find it of interest here is a speedy one that does not step through each character and barely uses offset.

David Epstein

function offsetPair a,b,str -- NOTE: a and b are single characters, str cannot be more than one line -- in string str, locate the first a, and the nth b, where n = the number of a's that precede that b -- e.g., if a = "(" and b = ")" the character positions of the first matched pair will be returned
-- return 0 if a is not found and empty if no match is found
  put offset(a,str) into ca
  if ca = 0 then return 0
put numToChar(7) into char 1 to ca of str -- some char that's not a or b
  replace a with return in str
  put 1 into na -- number (ordinal) of a's, also the line count in str
  put 0 into nb
  repeat for each line k in str
if k is not empty then -- does this line have (na-nb) instances of b?
      get nthHit(na-nb,b,k,z,numToChar(7))
if it is not empty then return ca && it+ca+length(line 1 to na-1 of str)
      add z to nb
    end if
    add 1 to na
  end repeat
  return empty
end offsetPair

function nthHit n,a,str,@z
-- returns character number of the nth instance of a (a single character) in str
  -- z reports the total number of hits
put numToChar(7) & str & numToChar(7) into str -- so str does not begin or end with a
  set itemDelimiter to a
  put -1 + the number of items in str into z
  if n > z then return empty
  return length(item 1 to n of str)
end nthHit

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to