Perl check brackets

This script looks how many Clips are left open or close and prints the missing brackets or okay if it matches right.

use strict;

print checkClip("5 (( +3").$/;
print checkClip("5 )) +3").$/;
print checkClip("5 ((3 -) 8 (4 +) 7)").$/;

# check brackets
sub checkClip {
my $clipcount = 0;
    while($_[0] =~ /G(.)/g) {
    ++$clipcount if($1 eq "(");
    --$clipcount if($1 eq ")");
    }
    if($clipcount == 0){
        return("seems good!");
    }elsif($clipcount > 0){
        return("there are $clipcount open cip too much!");
    }elsif($clipcount < 0){
        return("there are @{[$clipcount*-1]} close cip too much!");
    }
}

And we see how many clips we need to close or open, or if its looks good.

there are 2 open cip too much!
there are -2 close cip too much!
seems good!

 

 

Leave a Reply

Your email address will not be published.

eleven − 7 =