Finding matched parentheses

Geoff Canyon gcanyon at gmail.com
Sun Jul 28 03:54:50 EDT 2013


I checked, and your solution and my strictTotalMatching function return
identical results. It took me a while to figure out how it works. It's very
clever, but interestingly, not very efficient -- it almost certainly
doesn't matter for the likely use cases, but if you throw a string with a
few thousand (s and )s at it, seconds will go by before you get the result.

gc


On Fri, Jul 26, 2013 at 12:02 PM, <dunbarx at aol.com> wrote:

> I am old fashioned. And late. I think this works:
>
>
>
> on mouseUp
>    answer findNests(fld 1)
> end mouseUp
>
>
> function findNests var
>    repeat with y = 1 to the number of chars in var
>       if char y of var = "(" then put y & return after lefts --build list
> of left paren
>       if char y of var = ")" then put y & return after rights --build list
> of right paren
>    end repeat
>    put 0 & return before rights
>
>    repeat with y = the number of lines of lefts down to 1
>       repeat with u = the number of lines of rights down to 1
>          if line y of lefts < line u of rights and line y of lefts > line
> u-1 of rights then
>             put line y of lefts & "," & line u of rights & return after
> accum
>             delete line y of lefts
>             if line u of rights <> 0 then delete line u of rights
>          end if
>       end repeat
>    end repeat
>    sort accum numeric
>    return accum
> end findNests
>
>
> The returned list orders the char numbers of the paired parens.
>
>
> Craig
>
>
>
> -----Original Message-----
> From: Peter Haworth <pete at lcsql.com>
> To: How to use LiveCode <use-livecode at lists.runrev.com>
> Sent: Fri, Jul 26, 2013 12:09 pm
> Subject: Re: Finding matched parentheses
>
>
> On Fri, Jul 26, 2013 at 12:10 AM, Thierry Douez <th.douez at gmail.com>
> wrote:
>
> > But this one will work and match the first occurence:
> >
> >   if matchChunk(mystring,"(\([^)]*\)).*",tstart,tEnd)
> >
> > Regards,
> >
> > Thierry
> >
>
> Nice Thierry!  Doesn't handle nested parens though, but once again, that
> may not be a possibility in the OP's scenario.
>
> Pete
> lcSQL Software <http://www.lcsql.com>
> _______________________________________________
> 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
>
>
> _______________________________________________
> 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