in reply to Using qw to check for balanced parentheses
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using qw to check for balanced parentheses
by Anonymous Monk on Feb 01, 2011 at 23:16 UTC | |
by jwkrahn (Abbot) on Feb 02, 2011 at 01:08 UTC | |
by ikegami (Patriarch) on Feb 02, 2011 at 02:00 UTC | |
by Anonymous Monk on Feb 02, 2011 at 01:13 UTC | |
by Your Mother (Archbishop) on Feb 02, 2011 at 01:29 UTC | |
by Anonymous Monk on Feb 02, 2011 at 01:46 UTC | |
by unix_hacker_beard (Novice) on Feb 02, 2011 at 01:27 UTC | |
|
Re^2: Using qw to check for balanced parentheses
by Anonymous Monk on Oct 23, 2014 at 16:07 UTC |