in reply to Using qw to check for balanced parentheses
It took a long time and didn't give me the right answer for some reason:
Is );system('rm -rf /');# balanced? ~$#& NO CARRIER
Update: While I was trying to get back into that server I whipped up how I'd do this:
sub balanced { my( $str )= @_; my $d= 0; while( $str =~ m(([(])|([)]))g ) { if( $1 ) { $d++; } elsif( --$d < 0 ) { return 0; } } return 0 == $d; }
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using qw to check for balanced parentheses (oops)
by jdporter (Paladin) on Sep 16, 2011 at 19:06 UTC | |
by ikegami (Patriarch) on Sep 16, 2011 at 19:51 UTC | |
by Anonymous Monk on Oct 23, 2014 at 16:04 UTC | |
by tye (Sage) on Sep 16, 2011 at 19:42 UTC |