I was playing around with qw today and it got me thinking... I'm probably not the first one to do this but I couldn't find it anywhere else. This works since qw can take any non-alphanumeric separator.

sub is_balanced { my $string = shift; eval("qw($string)"); return 0 if $@; return 1; }

Here it is in action:

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", balanced_parens($balanced) ? "ye +s" : "no"); printf("Is $unbalanced1 balanced? %s\n", balanced_parens($unbalanced1) + ? "yes" : "no"); printf("Is $unbalanced2 balanced? %s\n", balanced_parens($unbalanced2) + ? "yes" : "no");

prints:

Is (1 + 2 * (3 + (5/4) + 8) + 6) balanced? yes Is (((1 + 2 * (3 + (5/4) + 8) + 6)) balanced? no Is (((1 + 2 * (3 + (5/4) + 8) + 6)))) balanced? no

Replies are listed 'Best First'.
Re: Using qw to check for balanced parentheses (oops)
by tye (Sage) on Feb 02, 2011 at 04:07 UTC

    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        

      sub balanced { local $_ = shift; s/[^()]//g; eval "qw($_)"; ! $@ }

        Simplified return value and shortened code by avoiding needless use of $_:

        sub balanced { (my $s = shift) =~ s/[^()]//g; eval "qw($s); 1" }

        Or in 5.14+:

        sub balanced { my $s = shift =~ s/[^()]//rg; eval "qw($s); 1" }

        Very nice!

        - tye        

Re: Using qw to check for balanced parentheses
by ikegami (Patriarch) on Feb 02, 2011 at 02:01 UTC
Re: Using qw to check for balanced parentheses
by jwkrahn (Abbot) on Feb 01, 2011 at 23:12 UTC

    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
      typo @$; is not $@;

        Yes, that works a lot better.   :-)

        Of course, the subroutine could be reduced to just:

        sub is_balanced { eval "qw($_[0])" }

        And it will also work with qq as well as with qw.

        $ perl -le' sub is_balanced { eval qq($_[0]) } 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))))"; print "Is $balanced balanced? ", is_balanced( $balanced ) ? "yes" : "n +o"; print "Is $unbalanced1 balanced? ", is_balanced( $unbalanced1 ) ? "yes +" : "no"; print "Is $unbalanced2 balanced? ", is_balanced( $unbalanced2 ) ? "yes +" : "no"; ' Is (1 + 2 * (3 + (5/4) + 8) + 6) balanced? yes Is (((1 + 2 * (3 + (5/4) + 8) + 6)) balanced? no Is (((1 + 2 * (3 + (5/4) + 8) + 6)))) balanced? no
        D'oh! Thanks, I'm too used to dereferencing arrays.
      I may be mistaken, but this does not detect a case like "}a{" , or does it? how about
      sub balanced { (my $s = shift) =~ s/[^()]//g; while ( $s =~s /\(\)// ){}; return $s ? 0 : 1; }
      ? It may be slower, but detects ')(' as incorrect