Results 1 to 18 of 18

Thread: Simple code.. what's wrong with it?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Question Simple code.. what's wrong with it?

    Hey guys, noob here.. I'm learning to work with arrays but I have a problem with my code somewhere...

    My goal is to:
    - load a text file
    - grab the first 15 characters of each line and store to strIP each time
    - see if the new value of strIP matches a value already in the array (arrIPs)
    - if it doesn't, then add it..
    - if it does, then add 1 to the count for this instance
    That last one is my problem. I keep getting inaccurate numbers.
    I.E. when something shows up in the text file about 34 times.. it is only showing a count of 2 or 1. I'm not sure what i've done wrong. Could ne1 help?

    Everything works according to plan except for the counting.
    As you can see, once the file is done loading, I display the count in another text box.... an inaccurate count.. grrrr

    VB Code:
    1. Open cd1.FileName For Input As #1
    2. Do Until EOF(1)
    3.     Line Input #1, strNewLine
    4.     ' get the ip string from the start of the log
    5.     strIP = (Mid(strNewLine, 1, txtCharsToCopy))
    6.  
    7.     'loop through the array to see if the ip is in it
    8.     Found = False
    9.     For i = 0 To numIPs
    10.         If strIP = arrIPs(i) Then
    11.             Found = True
    12.             arrIPcount(i) = (arrIPcount(i) + 1)
    13.             Exit For
    14.         End If
    15.     Next i
    16.  
    17.     If Found = False Then
    18.         arrIPs(numIPs) = strIP
    19.         numIPs = numIPs + 1
    20.         ReDim Preserve arrIPs(numIPs)
    21.         ReDim Preserve arrIPcount(numIPs)
    22.         txtSimple = txtSimple & strIP & vbCrLf
    23.     End If
    24. '========================
    25.  
    26. Loop
    27. Close #1
    28. For i = 1 To UBound(arrIPcount)
    29.     'compensate for 0 starting count
    30.     txtIPCount = txtIPCount & arrIPcount(i) & vbCrLf
    31. Next i
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple code.. what's wrong with it?

    Code:
    Open cd1.FileName For Input As #1
    Do Until EOF(1)
        Line Input #1, strNewLine
        ' Get the ip String from the start of the log
        strIP = (Mid(strNewLine, 1, txtCharsToCopy))
    
        'loop through the array To see If the ip is In it
        Found = False
        For i = 0 To ubound(arrIPs)
            If strIP = arrIPs(i) Then
                Found = True
                arrIPcount(i) = (arrIPcount(i) + 1)
                Exit For
            End If
        Next i
    
        If Found = False Then
           ReDim Preserve arrIPs(i)
           ReDim Preserve arrIPcount(i)
           arrIPs(I) = strIP
           arrIPcount(i)=1
           txtSimple = txtSimple & strIP & vbCrLf
        End If
    '========================
    
    Loop
    Close #1
    For i = 1 To UBound(arrIPs)
        'compensate For 0 starting count
        txtIPCount = txtIPCount & arrIs(i) & ": " & arrIPcount(i) & vbCrLf
    Next i
    This should work a little better. I didn't test it, though. Post back if there are any problems. You were close.

  3. #3
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Simple code.. what's wrong with it?

    One change I would make is to do:
    Code:
    Dim lngFreeFile As Long
    On Error Goto ErrHandler
       lngFreeFile = FreeFile
       Open cd1.FileName For Input As #lngFreeFile
    
       Close #lngFreeFile
       Exit Sub
    ErrHandler:
       Close #lngFreeFile
    That will stop the file failing to open next time you run your code after an error.

    WOka

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple code.. what's wrong with it?

    I thought about that, but not forgot about it until I re-read the thread.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Smile Re: Simple code.. what's wrong with it?

    hmmm... i don't know how you guys do it... lol... my brain ties itself into knots when i try to learn visual basic.. i can't even imagine C or Perl or something.. lol

    it looks like that code works great! Thanks a gabazabillion!

    i'll have to try that filename thing.. hmm.. i never had any issues opening the file back up, but i'm sure it wouldn't hurt to throw that in there

    P.S. Just out of curiosity. I'm writing this program for analysing an apache log file just for fun but.. do you guys know of a free Analys(z)er for Apache?

    Cheers
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Simple code.. what's wrong with it?

    Try setting up a UDT to hold each IP address and the number of times it appears in your file. Your code would then look something like this:
    VB Code:
    1. ' Put the following in the general declarations section
    2. Private Type IPArray
    3.     IP As String
    4.     Count As Long
    5. End Type
    6. Dim arrIPs() As IPArray
    7.  
    8. ' Here's your sub
    9. '=================================================
    10. ReDim arrIPs(0)
    11. Open cd1.FileName For Input As #1
    12.  
    13. Do Until EOF(1)
    14.     Line Input #1, strNewLine
    15.     ' get the ip string from the start of the log
    16.     strIP = (Mid(strNewLine, 1, txtCharsToCopy))
    17.  
    18.     'loop through the array to see if the ip is in it
    19.     Found = False
    20.     For i = 0 To numIPs
    21.         If strIP = arrIPs(i).IP Then
    22.             Found = True
    23.             arrIPs(i).Count = arrIPs(i).Count + 1
    24.             Exit For
    25.         End If
    26.     Next i
    27.  
    28.     If Found = False Then
    29.         arrIPs(numIPs).IP = strIP
    30.         arrIPs(numIPs).Count = 1
    31.         numIPs = numIPs + 1
    32.         ReDim Preserve arrIPs(numIPs)
    33.     End If
    34.  
    35. Loop
    36. Close #1
    37.  
    38. For i = 0 To numIPs - 1
    39.     'compensate for 0 starting count
    40.     txtIPCount = txtIPCount & cstr(arrIPs(i).Count) & vbCrLf
    41.     txtSimple = txtSimple & arrIPs(i).IP & vbCrLf
    42. Next i
    I haven't tested it, but it should do the trick.
    Last edited by pnish; Jan 14th, 2005 at 07:55 PM.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple code.. what's wrong with it?

    Why add an array before you need it? Sometimes it is easier when using 0 based arrays, but not in this instance.

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Simple code.. what's wrong with it?

    Quote Originally Posted by dglienna
    Why add an array before you need it? Sometimes it is easier when using 0 based arrays, but not in this instance.
    Huh?
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Re: Simple code.. what's wrong with it?

    hmm just for fun i tried that second idea.. which is kinda what i had in mind to begin with using a 2 column array..

    but i got 'invalid qualifiier' on this part..

    VB Code:
    1. If strIP = arrIPs(i).IP Then
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

  10. #10
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Simple code.. what's wrong with it?

    Post your new code (including your type declaration).
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  11. #11
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Simple code.. what's wrong with it?

    I wouldn't declare the array before hand.
    I would leave it empty until I needed it, and I certainly wouldn't have a spare place holder for a future item. I would use:
    Code:
    Option Explicit
    
    Private Declare Function SafeArrayGetDim Lib "oleaut32" (ByRef saArray() As Any) As Long
     
    Private Sub Form_Load()
    Dim arrStrings()   As String
       CheckArray
       ReDim arrStrings(0) 
       CheckArray
       Erase arrStrings
       CheckArray  
    End Sub
    
    Private Sub CheckArray()
       If SafeArrayGetDim(arrStrings) = 0 Then
          MsgBox "Array Empty"
       Else
          MsgBox "Array NOT Empty"
       End If
    End Sub
    Woof

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple code.. what's wrong with it?

    Quote Originally Posted by pnish
    Huh?
    I like to resize the array right before I write to it, as opposed to when it may not even be needed. Sometimes causes a problem if you want to use the 0 (or first) array option, but you can set the index to -1 or some other tricks to handle it.

  13. #13
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Simple code.. what's wrong with it?

    Yeah. I see what you're saying now (and Woka too) and I agree with both of you. I originally did it this way (in a previous post by iqchicken) so I wouldn't have to rearrange his code too much.

    That's what you get for being lazy
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Smile Re: Simple code.. what's wrong with it?

    Well i figured out what I did wrong w/ pnish's code. It works great 2! You guys are awesome..

    That last suggestion would probably work also.. though I don't really understand anything that's going on..

    I'm still just a beginner.

    Thank you everybody! I'm one miniscule step closer to psuedo-proffesionalism.. yiii hoo..

    i tried that last suggestion and got a type mismatch on arrStrings in this sub:

    VB Code:
    1. Private Sub CheckArray()
    2.    If SafeArrayGetDim(arrStrings) = 0 Then
    3.       MsgBox "Array Empty"
    4.    Else
    5.       MsgBox "Array NOT Empty"
    6.    End If
    7. End Sub

    I'm sure I've just got something misplaced.
    Last edited by iqchicken; Jan 14th, 2005 at 08:44 PM.
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

  15. #15

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple code.. what's wrong with it?

    Have to put it above the first subroutine, in General Declarations.

    Code:
    Option Explicit
    Dim arrStrings()   As String
    Private Declare Function SafeArrayGetDim Lib "oleaut32" (ByRef saArray() As Any) As Long
     
    Private Sub Form_Load()
    
       CheckArray
       ReDim arrStrings(0)
       CheckArray
       Erase arrStrings
       CheckArray
    End Sub
    
    Private Sub CheckArray()
       If SafeArrayGetDim(arrStrings) = 0 Then
          MsgBox "Array Empty"
       Else
          MsgBox "Array Not Empty"
       End If
    End Sub
    I have no trouble knowing if the array is empty, but I always dim(0) with zero items, and then have a problem detecting if its occupied or not. I guess I could not redim it as anything until I need it.

  17. #17
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Simple code.. what's wrong with it?

    This is the code I have:
    Code:
    Option Explicit
    
    Private Declare Function SafeArrayGetDim Lib "oleaut32" (ByRef saArray() As Any) As Long
    
    Private Type mtypIPs
        IP      As String
        Count   As Long
    End Type
    
    Private mudtData() As mtypIPs
    
    Private Sub Command1_Click()
    On Error GoTo ErrHandler
        
        StartExample
        
        Exit Sub
    ErrHandler:
        MsgBox Err.Number & " - " & Err.Description, vbCritical, "Error"
    End Sub
    
    Private Sub StartExample()
    Dim lngIndex    As Long
    Dim strEntry    As String
    On Error GoTo ErrHandler
        
        List1.Clear
        GetData (App.Path & "\Data.txt")
        If SafeArrayGetDim(mudtData) = 0 Then
            MsgBox "No data found"
        Else
            For lngIndex = 0 To UBound(mudtData)
                strEntry = mudtData(lngIndex).IP & " - " & mudtData(lngIndex).Count
                List1.AddItem strEntry
            Next lngIndex
        End If
        
        Exit Sub
    ErrHandler:
        Err.Raise Err.Number
    End Sub
    
    Private Sub GetData(ByVal pstrFilename As String)
    Dim blnInit         As Boolean
    Dim strLine         As String
    Dim strIP           As String
    Dim blnFound        As Boolean
    Dim lngFreeFile     As Long
    Dim lngIndex        As Long
    On Error GoTo ErrHandler
    
        Erase mudtData
        lngFreeFile = FreeFile
        Open pstrFilename For Input As #lngFreeFile
        Do Until EOF(lngFreeFile)
            Line Input #lngFreeFile, strLine
            strIP = Mid$(strLine, 1, 15)
            blnFound = False
            If blnInit Then
                For lngIndex = 0 To UBound(mudtData)
                    If strIP = mudtData(lngIndex).IP Then
                        blnFound = True
                        mudtData(lngIndex).Count = mudtData(lngIndex).Count + 1
                        Exit For
                    End If
                Next lngIndex
            End If
            If Not blnFound Then
                If Not blnInit Then
                    lngIndex = 0
                    blnInit = True
                Else
                    lngIndex = UBound(mudtData) + 1
                End If
                ReDim Preserve mudtData(lngIndex) As mtypIPs
                mudtData(lngIndex).IP = strIP
                mudtData(lngIndex).Count = 1
            End If
        Loop
        Close #lngFreeFile
        
        Exit Sub
    ErrHandler:
        Close #lngFreeFile
        Err.Raise Err.Number
    End Sub
    It seems to work.
    Attached is the project I ran, with my test file with random data in.

    WOka
    Attached Files Attached Files

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple code.. what's wrong with it?

    That works well. I like the method, so I'll prolly use it again.
    Thanks.

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