1) i noticed in your earlier code you were using = when you should have been using ==
2) you also had a do statement without a corresponding while condition. avoid do/while's for this and other reasons. you should stick with "while" loops unless you have compelling reason to use "do/while"
3) you should not be "return"-ing in the "else" statement.
4) you should not attempt to define the end of a function by the appearance of "return"... besides the fact that void types do not return a value, a function can have any number of return statements.
otherwise, this sort of works in a basic sense:
use strict;
my $openCount;
my $closeCount;
my $foundReturn;
my $myCfile;
die "usage: perl $0 <filename>\n"
if (!($ARGV[0]));
die "file: <",$ARGV[0],"> does not exist\n"
if (! -e ($myCfile = $ARGV[0]));
open(fp,$myCfile);
open(writeme,">output.txt");
print "reading <",$myCfile,"> ...\n";
$openCount = $closeCount = $foundReturn = 0;
while(<fp>)
{
if ($_ =~ /return/) {
print "hey am inside return if\n";
$foundReturn=1;
}
elsif(($_ =~ /^\{/) || ($_ =~ /\{$/)) {
$openCount++;
print "openCount1=", $openCount ,"\n";
}
elsif(($_ =~ /^\}/) || ($_=~ /\}$/ )) {
$closeCount++;
print "closeCount=", $closeCount,"\n";
}
if($openCount==$closeCount) {
print "count is equal\n";
}
}
you STILL have the problem that if a brace is both preceded and followed by any combination of whitespace, comments, or additional code -- which is a VERY likely prospect -- your counting code will become immediately and irretreivably lost.
for instance:
int main (int argc, char *argv[])
{
unsigned *tempString, *tempPtr;
unsigned i=0, opers=0, ptr=0, key;
FILE *fp;
typedef struct Operator_s {
char Name[26];
char Oin[11];
char Password[16];
} Operator_t;
Last edited by jephthah : Mar 26th, 2008 at 5:28 pm.