It doesn't appear to work?
$ perl -le' sub is_balanced { my $string = shift; eval("qw($string)"); return 0 if @$; return 1; } my $balanced = "(1 + 2 * (3 + (5/4) + 8) + 6)"; my $unbalanced1 = "(((1 + 2 * (3 + (5/4) + 8) + 6))"; my $unbalanced2 = "(((1 + 2 * (3 + (5/4) + 8) + 6))))"; printf("Is $balanced balanced? %s\n", is_balanced($balanced) ? "yes" : + "no"); printf("Is $unbalanced1 balanced? %s\n", is_balanced($unbalanced1) ? " +yes" : "no"); printf("Is $unbalanced2 balanced? %s\n", is_balanced($unbalanced2) ? " +yes" : "no"); ' Is (1 + 2 * (3 + (5/4) + 8) + 6) balanced? yes Is (((1 + 2 * (3 + (5/4) + 8) + 6)) balanced? yes Is (((1 + 2 * (3 + (5/4) + 8) + 6)))) balanced? yes
In reply to Re: Using qw to check for balanced parentheses
by jwkrahn
in thread Using qw to check for balanced parentheses
by unix_hacker_beard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |