Useful, certainly, but it seems to me you're defining "unmatched braces" as a mismatch in the count over the whole document of left and right versions of the brace types. By my definition, at least, the term is stronger than that: braces must also be properly nested (i.e. '({)}' is invalid). I'd be more inclined to do something like the following (a fairly simple stack model), which catches such errors and as a bonus gives more information about where the error occurred:
#!/usr/bin/perl -w use strict; my %x = (')'=>'(','}'=>'{',']'=>'['); my @braces = (); while (<>) { s/\\(\(|\)|\{|\}|\[|\])//g; # yank out escaped braces for (split //, $_) { # split each line into chars push(@braces, $_), next if (/\(|\{|\[/); # push if left brace /(\)|\}|\])/ and # if right brace (@braces > 0 # and left braces on stack ? $braces[-1] eq $x{$1} # and right brace matches left ? pop @braces # pop the pair : (print("unmatched '$braces[-1]' before '$_' on line $."), e +xit) # saw a left without correct right : (print("no match for '$_' on line $."), exit) # saw a right + with no stack ); } } print "passed!\n";

This is barely tested, so nobody go using it anywhere. Granted, this doesn't take quoted braces into account, but that's a whole different story.


In reply to Re: (La)TeX brace counter (athomason) by athomason
in thread (La)TeX brace counter by Strahinja

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.