Gossamer Forum
Home : General : Perl Programming :

probably very simple RE problem

Quote Reply
probably very simple RE problem
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 :)

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";
any ideas? or if you know a better way, any help appreciated.
Quote Reply
Re: [mhammond] probably very simple RE problem In reply to
If it's a uni project then isn't this cheating Wink

This is a brief example that may help....

Code:
my $string = 'if ( if ( if ( if ( if ( if (';
my $count = 0;
$count++ while ($string =~ /if \(/g);
print $count;

...prints 6.

Last edited by:

Paul: Nov 29, 2002, 6:47 AM
Quote Reply
Re: [Paul] probably very simple RE problem In reply to
Slighly different solution:
Code:
my $string = 'if ( if ( if ( if ( if ( if (';
my @count = ($string =~ /if \(/g);
print scalar @count;
also prints 6 (and is more elegant)

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] probably very simple RE problem In reply to
Quote:
(and is more elegant)

Well that's a matter of opinion smarty pants Wink

What if the java file is huge...you'll waste memory Angelic

Last edited by:

Paul: Nov 29, 2002, 7:08 AM
Quote Reply
Re: [Paul] probably very simple RE problem In reply to
Quote:
What if the java file is huge...you'll waste memory
I'll just put a block around it { }, so that the var goes out of scope very quickly....
But of course you're right.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] probably very simple RE problem In reply to
Thanks guys both are a lot more elagent than the solution I finally came up with! ill see how yours works. Na it would be cheating if we had lab demonstrators that could actually help, they openly admit they wouldn't even know where to start!



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 (<TEXTFILE>) {
foreach $line ( split ) {
# do something with $word here

if ($line =~ /if\(/) {
$countif = $countif + 1;
print "1"
}

if ($line =~ /while\(/) {
$countwhile = $countwhile + 1;
print "1"
}

if ($line =~ /for\(/) {
$countfor = $countfor + 1;
print "1"
}
}
}

close(TEXTFILE);
print "\nThe file $filename contains\n";
print "$countif if loops\n";
print "$countwhile while loops\n";
print "$countfor for loops\n";

Thanks for your replies, I will give back when i actually know things :)

Last edited by:

Paul: Nov 29, 2002, 8:42 AM
Quote Reply
Re: [mhammond] probably very simple RE problem In reply to
Might wanna work out how the forum tags work first Laugh

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [mhammond] probably very simple RE problem In reply to
just for the sake of tmtowtdi:

Code:
my %counts = ();
s/(if|for|while)\s+\(/{$counts{$1}++}/ge while (<TEXTFILE>);
print "$_ count: $counts{$_}\n" for sort keys %counts;

single point of change to add/change control structures (and the perl golf points)

-g

s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';