My program is trying to read from a line and count the number of parentheses however, as you can see it's not working very well: My code runs, but does not work, as no matter what either both l_count and r_count are the same or it throws an error about the parentheses not being balanced properly...
How do I count the # of Parentheses in a string?
Edit: samarzone fixed me up right and showed me what I was doing wrong. It has thus been fixed:
sub parenth {
my ($string) = (@_);
my $l_count = () = ($string =~ /\(/g);
my $r_count = () = ($string =~ /\)/g);
if ($l_count != $r_count) {
print "There is no balance on this line! Did you format it
+ correctly???";
return 0;
}
else {
print "There is balance in this universe";
return 1;
}
}
Any thoughts on how to implement such parsing methods without regex (is that even possible?)
Or just ideas in general for uses, as such?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.