Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: IT's not counting...

by mjscott2702 (Pilgrim)
on Nov 16, 2010 at 08:46 UTC ( #871662=note: print w/replies, xml ) Need Help??

in reply to Re: IT's not counting...
in thread IT's not counting...

If you don't want to clobber the $testString contents, you could change as below, and add parentheses for clarity (not sure if that is returning in a list context, then assigning to a scalar, but I have seen that idiom many times before).

my $l_count = ($testString =~ tr/(/(/); my $r_count = ($testString =~ tr/)/)/);

Replies are listed 'Best First'.
Re^3: IT's not counting...
by johngg (Abbot) on Nov 16, 2010 at 15:30 UTC

    As jwkrahn had pointed out, the string is not clobbered by his code. You need to supply the d flag to actually delete the characters.

    knoppix@Microknoppix:~$ perl -E ' > $str = q{3 * (4 + 5) / (6 - 7)}; > say $str; > $lb = $str =~ tr/(//; > $rb = $str =~ tr/)//; > say qq{left : $lb}; > say qq{right: $rb}; > say $str; > $dig = $str =~ tr/0-9//d; > say qq{digit: $dig}; > say $str;' 3 * (4 + 5) / (6 - 7) left : 2 right: 2 3 * (4 + 5) / (6 - 7) digit: 5 * ( + ) / ( - ) knoppix@Microknoppix:~$

    I hope this is helpful.



Re^3: IT's not counting...
by jwkrahn (Monsignor) on Nov 16, 2010 at 15:12 UTC
    If you don't want to clobber the $testString contents


    my $l_count = $testString =~ tr/(//; my $r_count = $testString =~ tr/)//;


    my $l_count = ($testString =~ tr/(/(/); my $r_count = ($testString =~ tr/)/)/);

    do exactly the same thing.    Neither of which "clobbers" the string.

    not sure if that is returning in a list context

    The left-hand side of the assignment determines context, in this case scalar, so the parentheses are irrelevant.

Re^3: IT's not counting...
by Dwood (Acolyte) on Nov 16, 2010 at 08:59 UTC
    I made an oops before I posted!

    $testString is supposed to be the local $string! A leftover from when I was just trying to get it to work at all haha! Which gives us more room to work with since you know that we can do whatever we want as long as the number of parentheses are counted!

    And I don't really want to use too much regex (not much understanding of regex) if I can avoid it. (Why on earth did I try making a simple line parser? :P )

    As a note, samar pointed me to what I think I'll need... but eventually the concept will have to be expanded eventually to fit my needs...
      If you want to find out whether parenthesis are balanced in a string, you do not need a complicated regexp. In fact, you could even do it without a regexp at all, but let me present you a solution with a simple regexp:
      sub is_balanced { my $_ = shift; my $c = 0; while (/([()])/g) { if ($1 eq '(') {$c++; next} return if --$c < 0; } $c == 0; }
      The trick is to keep a counter. Increase the counter by 1 each time you see a '(', decrement the counter by 1 each time you see a ')'. If the counter ever becomes less than 0, or if the counter isn't 0 when reaching the end of the string, the parens aren't balanced. Otherwise, they are.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://871662]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-01-20 23:15 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.