#!/usr/bin/perl # Description: Check string for unmatching parenthesis # # Last Update: 23 Aug 2011 # Designed by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # Coded by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # # Copyright 2006-2015 Dusan Baljevic # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . my $s = '((()))()'; my $s1= ')()()()())))(()()()(('; my @ARR = ( $s, $s1 ); my $openp = '('; my $closp = ')'; foreach my $arrel ( @ARR ) { print "\nChecking string \"$arrel\"\n"; my $offset1 = my $offset2 = 0; my $count1 = my $count2 = 0; my $result1 = index($arrel, $openp, $offset1); my $result2 = index($arrel, $closp, $offset2); while ($result1 != -1) { $count1++; $offset1 = $result1 + 1; $result1 = index($arrel, $openp, $offset1); } while ($result2 != -1) { $count2++; $offset2 = $result2 + 1; $result2 = index($arrel, $closp, $offset2); } if ( $count1 == $count2 ) { print "String $arrel contains equal number of open and close parenthesis ($count1)\n"; } else { print "String $arrel contains non-equal number of open and close parenthesis ($count1 versus $count2)\n"; } } exit(0);