Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

RE: RE: Re: getting a return code from a looping regular expression

by BlaisePascal (Monk)
on Aug 01, 2000 at 22:57 UTC ( [id://25541]=note: print w/replies, xml ) Need Help??


in reply to RE: Re: getting a return code from a looping regular expression
in thread getting a return code from a looping regular expression

True, PDA's are the proper tool for balancing parenthesis. But it helps if it works... Your PDA would match "))((" perfectly find. Most wouldn't consider that balanced...
sub balanced { my $string = shift; my $stack = 0; @string = split //, $string; for (@string) { last if $stack < 0; $stack++ if /[(]/; $stack-- if /[)]/; } return $stack ? 0 : 1; }
or, without using split and //:
sub balanced { my $string = shift; my $stack = 0; for my $i (0..length($string)-1) { last if $stack < 0; $stack++ if substr($string,$i,1) eq '('; $stack-- if substr($string,$i,1) eq ')'; } return $stack ?0 : 1; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://25541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-28 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found