Results 1 to 7 of 7

Thread: Counting loops and Ifs in a piece of code (Resolved)

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    5

    Talking 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:
    1. Dim strText As String
    2. Dim intCounter1 As Integer
    3. Dim intCounter2 As Integer
    4. Dim intCounter3 As Integer
    5.  
    6. Open "C:\blah\blah\something.txt" For Input As #1
    7.  
    8. While Not EOF(1)
    9.  
    10.   Line Input #1, strText
    11.  
    12.     If InStr(strText, "End If") <> 0 Then
    13.       intCounter1 = intCounter1 + 1
    14.     End If
    15.    
    16.     If InStr(strText, "Loop") <> 0 Then
    17.       intCounter2 = intCounter2 + 1
    18.     End If
    19.    
    20.     If InStr(strText, "Wend") <> 0 Then
    21.       intCounter3 = intCounter3 + 1
    22.     End If
    23.    
    24. Wend
    25.  
    26. txtIf.Text = intCounter1
    27. txtFor.Text = intCounter2
    28. txtWhile.Text = intCounter3
    29. txtTotal.Text = intCounter1 + intCounter2 + intCounter3
    30.  
    31. Close #1
    32.  
    33. End Sub

    Thanks in advance for any advice or tips.
    Last edited by abe; Oct 13th, 2003 at 06:15 AM.

  2. #2
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    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

  3. #3

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    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.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Counting loops and Ifs in a piece of code

    Here's a slight modification
    VB Code:
    1. '...
    2. Do While Not EOF(1)
    3.  
    4.   Line Input #1, strText
    5.  
    6.    'Remove spaces on either side of the text, and convert to uppercase
    7.    '(easier to check it safely)
    8.   strText = Ucase(Trim(strText))
    9.  
    10.                                         'Make sure it's not a comment
    11.   If Left(strText,1) <> "'" And Ucase(Left(strText,4)) <> "REM " Then
    12.  
    13. 'Using InStr allows the text to be part of a string, left makes sure its the
    14. 'first part of the line (after the spaces, which we've removed already)
    15.         If Left(strText, 3) = "IF " Then
    16.           intCounter1 = intCounter1 + 1
    17.         End If
    18.    
    19. 'these are a little more awkward, since they may or may not have code after
    20. '(eg:  "Loop" is valid, so is "Loop While x=1")
    21.         If Left(strText,5) = "LOOP " or strText = "LOOP" Then
    22.           intCounter2 = intCounter2 + 1
    23.         End If
    24.  
    25.         If Left(strText,5) = "WEND " or strText = "WEND" Then
    26.           intCounter3 = intCounter3 + 1
    27.         End If
    28.     End If
    29.    
    30. Loop  '(I dont like Wend, can't remember why!)
    31.  
    32. txtIf.Text = intCounter1
    33. '...

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    5
    Thanks very much si_the_geek. I appreciate that.
    Cheers for the comments from the other as well

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width