Finding matched parentheses
Peter M. Brigham
pmbrig at gmail.com
Fri Jul 26 09:17:00 EDT 2013
Not sure what you are looking for here. Just find first occurrence of a in str and return the offset of the next occurrence of b after that?
function offsetPair a,b,str, at z
-- returns offset(a,str) and loads in z offset(b,str) where b is the "matching" member of the pair;
-- returns 0 if there is no a, empty if there is no match
-- offsets(b,str) returns a comma-delimited list of all the offsets
-- of a in str (see below)
put offset(a,str) into startOffset
if startOffset = 0 then return 0
put offsets(b,str) into endOffsets
if endOffsets = 0 then return empty
repeat with i = 1 to the number of items of endOffsets
if item i of endOffsets < startOffset then next repeat
put item i of endOffsets into z
exit offsetPair
end repeat
return empty
end offsetPair
Actually, if str is grammatically correct, this should always return item 1 of endOffsets.
If you want a more general solution, you could use offsets(a,str) along with offsets(b,str) and look at all the pairs, with item i of offsets(b,str) being index of the closing paren for the open paren at item i of offsets(a,str). Presuming that there are no unmatched parens in str, the latter should be greater than the former for each i.
Also, I'd be inclined to return the match as the value of the function call, instead of putting it into a referenced variable. Otherwise why not use a command instead of a function?
But again, I'm not sure what you are looking for exactly.
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
PS: I've posted this before, but at the risk of repeating myself:
function offsets str,container,includeOverlaps
-- returns a comma-delimited list of all the offsets of str in container
-- returns 0 if not found
-- third param is optional:
-- offsets("xx","xxxxxx") returns "1,3,5" not "1,2,3,4,5"
-- ie, by default, overlapping offsets are not counted
-- if you want overlapping offsets then pass "true" in 3rd param
if str is not in container then return 0
if includeOverlaps = empty then put false into includeOverlaps
put empty into offsetList
put 0 into startPoint
repeat
put offset(str,container,startPoint) into thisOffset
if thisOffset = 0 then exit repeat
add thisOffset to startPoint
put startPoint & comma after offsetList
if not includeOverlaps then
add length(str)-1 to startPoint
end if
end repeat
return item 1 to -1 of offsetList -- delete trailing comma
end offsets
On Jul 25, 2013, at 8:17 PM, David Epstein wrote:
> Has anyone scripted a function that will locate the closing parenthesis (or bracket, etc.) of a pair? Below is my effort. Reports of its limitations or simpler alternatives (regex?) are invited.
>
> David Epstein
>
> function offsetPair a,b,str, at z
> -- returns offset(a,str) and loads in z offset(b,str) where b is the "matching" member of the pair;
> -- returns 0 if there is no a, empty if there is no match
>
> if a is not in str then return 0
> if b is not in str then return empty
> put 0 into b4 -- skipped characters
> put 0 into na -- number (ordinal) of the a hit
> put 0 into nb
> repeat
> put offset(a,str,b4) into ca -- character position of a
> put offset(b,str,b4) into cb -- character position of a
> if ca = 0 and na < nb then return empty
> if cb = 0 and nb < na then return empty
> -- only take note of the first hit:
> put empty into hitCase -- default
> if ca > 0 and (ca < cb or cb = 0) then put "a" into hitCase
> if cb > 0 and (cb < ca or ca = 0) then put "b" into hitCase
> if hitCase = "a" then
> add 1 to na
> put ca+b4 into aca[na] -- absolute char position of a hit number na
> put aca[na] into b4
> else if hitCase = "b" then
> add 1 to nb
> put cb+b4 into acb -- absolute char position of most recent b hit
> put acb into b4
> else return empty
> if na = nb then -- we have a match
> put acb into z
> return aca[1]
> end if
> end repeat
> return empty
> end offsetPair
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list