hey, I have a problem for uni to find all the instances of control structures in a java source file, I wrote this code that sucessfully finds all but those on the same line, which apparently loose us 40%, surly it can't be that difficult, I can't see why it gives up after it finds one is there a way of forcing it to look at the whole line? Basically im trying to do grep "for(" wc -w it is enough to just check the for( part apparently not that they are closed or anything. I've tried foreach and while ! end of line type things but I don't really enough to get the syntax right. Please help :)
#!/usr/bin/perl
$countif = 0;
$countwhile = 0;
$countfor = 0;
print"Please type the name of the java file: ";
$filename = <STDIN>;
open(TEXTFILE,"$filename") || die "Can't Open $filename: $!\n";
while ($line=<TEXTFILE>) {
chop($line);
if ($line =~ /if\(/) {
$countif = $countif + 1;
print "1"
}
if ($line =~ /while\(/) {
$countwhile = $countwhile + 1;
}
if ($line =~ /for\(/) {
$countfor = $countfor + 1;
}
}
close(TEXTFILE);
print "\nThe file $filename contains\n";
print "$countif if loops\n";
print "$countwhile while loops\n";
print "$countfor for loops\n";any ideas? or if you know a better way, any help appreciated.
Code:
#!/usr/bin/perl
$countif = 0;
$countwhile = 0;
$countfor = 0;
print"Please type the name of the java file: ";
$filename = <STDIN>;
open(TEXTFILE,"$filename") || die "Can't Open $filename: $!\n";
while ($line=<TEXTFILE>) {
chop($line);
if ($line =~ /if\(/) {
$countif = $countif + 1;
print "1"
}
if ($line =~ /while\(/) {
$countwhile = $countwhile + 1;
}
if ($line =~ /for\(/) {
$countfor = $countfor + 1;
}
}
close(TEXTFILE);
print "\nThe file $filename contains\n";
print "$countif if loops\n";
print "$countwhile while loops\n";
print "$countfor for loops\n";