|
-
Oct 12th, 2003, 11:40 AM
#1
Thread Starter
New Member
Counting loops and Ifs in a piece of code (Resolved)
hi everyone
Im quite new to vb so please bear with me.
What I would like to do is write a piece of vb code
which will read in another piece of vb code line by line.
But it needs to be able to count how many occurances
of a loop and IF statements which appear in that code.
This must however ignore any IFs or similar words that
may appear in any comments or within a string.
Hope that makes sense.
So far ive managed to write a very simple piece of code
which counts the number of FORs, IFs, and WHILEs.
VB Code:
Dim strText As String
Dim intCounter1 As Integer
Dim intCounter2 As Integer
Dim intCounter3 As Integer
Open "C:\blah\blah\something.txt" For Input As #1
While Not EOF(1)
Line Input #1, strText
If InStr(strText, "End If") <> 0 Then
intCounter1 = intCounter1 + 1
End If
If InStr(strText, "Loop") <> 0 Then
intCounter2 = intCounter2 + 1
End If
If InStr(strText, "Wend") <> 0 Then
intCounter3 = intCounter3 + 1
End If
Wend
txtIf.Text = intCounter1
txtFor.Text = intCounter2
txtWhile.Text = intCounter3
txtTotal.Text = intCounter1 + intCounter2 + intCounter3
Close #1
End Sub
Thanks in advance for any advice or tips.
Last edited by abe; Oct 13th, 2003 at 06:15 AM.
-
Oct 12th, 2003, 12:05 PM
#2
You'll need some Trim's, perhaps get rid of preceeding vbTab chars, and be able to Detect and ignore Commented segments. But it looks like you're headed in the right direction.
Perhaps use Text Arrays holding the keywords you want to detect, doing a looping comparison instead of a whole series of individual if statements, and incrimenting another array at the index when you do get a match. And, also, instead of individual text boxes for displaying results, use a control array of text boxes, again allowing you to loop to output your results.
Perhaps you'd like to include Position counters, so you know which lines in the inspected code contain your hits?
-Lou
-
Oct 12th, 2003, 04:34 PM
#3
Believe it or not, VB still supports BASIC's way of inluding comments so when looking for them you might want to watch out for something like
-
Oct 12th, 2003, 05:16 PM
#4
Don't forget about the single line If statement. Your existing code will not count those because a single line If does not require an End If.
-
Oct 13th, 2003, 04:32 AM
#5
Re: Counting loops and Ifs in a piece of code
Here's a slight modification
VB Code:
'...
Do While Not EOF(1)
Line Input #1, strText
'Remove spaces on either side of the text, and convert to uppercase
'(easier to check it safely)
strText = Ucase(Trim(strText))
'Make sure it's not a comment
If Left(strText,1) <> "'" And Ucase(Left(strText,4)) <> "REM " Then
'Using InStr allows the text to be part of a string, left makes sure its the
'first part of the line (after the spaces, which we've removed already)
If Left(strText, 3) = "IF " Then
intCounter1 = intCounter1 + 1
End If
'these are a little more awkward, since they may or may not have code after
'(eg: "Loop" is valid, so is "Loop While x=1")
If Left(strText,5) = "LOOP " or strText = "LOOP" Then
intCounter2 = intCounter2 + 1
End If
If Left(strText,5) = "WEND " or strText = "WEND" Then
intCounter3 = intCounter3 + 1
End If
End If
Loop '(I dont like Wend, can't remember why!)
txtIf.Text = intCounter1
'...
this doesn't take into account the problem of using : to combine multiple lines of code onto one line, but it's a little better
-
Oct 13th, 2003, 04:55 AM
#6
Thread Starter
New Member
Thanks very much si_the_geek. I appreciate that.
Cheers for the comments from the other as well
-
Oct 13th, 2003, 09:45 AM
#7
Don't foregt that when you consider comments you also need to consider cases like this and not count the For and Next in the comment
VB Code:
For X = 1 to 5 ' The For Next statement purpose is...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|