in reply to finding { and }

Your problem's been addressed. But... rather than split-ing the string, and examining individual characters, why not use index? That removes the problem with regexps and brackets, as well as being faster.

If $brackets ever goes below 0, you've got a problem, even if the final total is ok: consider )(. And you're not considering brackets inside strings:printf("(");

Of course, the logical extension is to consider the number of round brackets inside each pair of curly brackets. At this rate you're going to end up with a full blown syntax checker. Why not hand the whole thing over to the compiler?

Replies are listed 'Best First'.
Re^2: finding { and }
by Aristotle (Chancellor) on Oct 17, 2001 at 07:14 UTC
    Or if you still insist on doing it yourself, you might want to use Text::Balanced which does exactly what you need in ways you probably haven't even thought of yet. Or even Parse::RecDescent if you can handle it, but in that case you're probably getting so close to doing your compiler's work that you really should hand the files over to it instead.