Results 1 to 14 of 14

Thread: Simple code.. What's wrong?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Unhappy Simple code.. What's wrong?

    Ok.. What I'm trying to do is:

    1. open a log file - CHECK
    2. grab the first 15 characters of each line (an IP address) - CHECK
    3. bounce these first 15 off of my array to see if they are in it, if not, then record them in the array (arrIPs)
    4. Print the new IP.. only if it is one i haven't encountered thus far.


    The problem:
    It records the first IP and prints it out just fine, but then the second one prints in the text box multiple times.. as does the 3rd, 4th, etc..

    What am i doing wrong?? Any help at all is greatly appreciated. I'm a noob! But I don't wanna be!



    VB Code:
    1. Private Sub cmdLogalyze_Click()
    2. Dim arrIPs(1000) As Variant
    3. strwrap = Chr$(13) + Chr$(10)
    4. Chr$ (10)
    5. txtSimple = ""
    6. cd1.InitDir = "C:\Program Files\Apache Group\Apache2\logs"
    7. cd1.Filter = "Log Files (*.log)|*.log"
    8. cd1.ShowOpen
    9. If cd1.FileName = "" Then
    10. msg = MsgBox("No File Chosen.", vbInformation, "File Needed")
    11. Exit Sub
    12. End If
    13.  
    14.  
    15. Open cd1.FileName For Input As #1
    16. Do Until EOF(1)
    17. Line Input #1, strNewLine
    18. ' get the ip string from the start of the log
    19. strIP = (Mid(strNewLine, 1, 15))
    20.  
    21. 'loop through the array to see if the ip is in it
    22. For i = 1 To UBound(arrIPs)
    23. ' if it is, then go to the next line
    24. If strIP = arrIPs(i) Then
    25. GoTo skipwrite
    26. ' if not, then record it in the array
    27. Else
    28.  
    29. 'loop through array to find free spot for next ip
    30. For j = 1 To UBound(arrIPs)
    31. If arrIPs(j) = "" Then arrIPs(j) = strIP: GoTo PrintIP
    32. Next j
    33. End If
    34.  
    35. Next i
    36. PrintIP:
    37. txtSimple = txtSimple & strIP & strwrap
    38.  
    39. skipwrite:
    40. Loop
    41. Close #1
    42. End Sub
    Last edited by iqchicken; Jan 6th, 2005 at 08:36 PM. Reason: Simplification..
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Simple code.. What's wrong?

    First: use indentations - code is difficult to read.
    Second: you are searching from and writing to THE SAME ARRAY - arrIPs().

    As far as I could understand your LOOKUP array should be different from that you need to populate ...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Re: Simple code.. What's wrong?

    would the fact that it is the same array break the operation of it? i didn't think it would..

    and i personally hate indents .. i like reading it all inline..it's also a extremely small chunk of code, so i didn't think it would be a problem reading.

    but i will indent from now on cuz i guess most people like it better.. sorry about that..

    thanks for responding.. but does ne1 have any clues to why this code won't work?
    Last edited by iqchicken; Jan 6th, 2005 at 09:42 PM.
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

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

    Re: Simple code.. What's wrong?

    Don't your arrays start at 0? If so, you are missing them.

    Also, you don't set the new array position as being +1 to the last one.
    You should redim with the preserved option before you write to the array.

    Start out like this:

    Dim arrIPs() As Variant

    set the first position as, say for example, z=0

    when you go to add it

    redim preserve arrIPs(z + 1)

    you wouldn't need to loop to find the next open spot.
    Last edited by dglienna; Jan 6th, 2005 at 10:09 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Re: Simple code.. What's wrong?

    If strIP = arrIPs(i)

    this is where my index was being incremented.. so actually i WAS adding one each time..


    I've never used the preserve thing.. i guess i'll have to read up on it.. i don't really know to impliment it.

    thanks though.

    this is what i'm still getting..

    xx-xxx-xxx-xxx
    yy-yyy-yyy-yyy
    yy-yyy-yyy-yyy
    yy-yyy-yyy-yyy
    yy-yyy-yyy-yyy

    the first ip it comes across is never repeated.. but something is not working as far as my storing a new ip into the array...

    ne other advice?


    here is the problem code:

    VB Code:
    1. Do Until EOF(1)
    2.     Line Input #1, strNewLine
    3.     ' get the ip string from the start of the log
    4.     strIP = (Mid(strNewLine, 1, 15))
    5. '============================================== PROBLEM
    6.     'loop through the array to see if the ip is in it
    7.         For i = 0 To UBound(arrIPs)
    8.             ' if it is, then go to the next line
    9.             If strIP = arrIPs(i) Then
    10.                 GoTo skipwrite
    11.                 ' if not, then record it in the array
    12.             Else
    13.  
    14.                 'loop through array to find free spot for next ip
    15.                 For j = 0 To UBound(arrIPs)
    16.                     If arrIPs(j) = "" Then
    17.                         arrIPs(j) = strIP
    18.                         txtSimple = txtSimple & strIP & strwrap
    19.                         GoTo skipwrite
    20.                     End If
    21.                 Next j
    22.             End If
    23.        
    24.         Next i
    25. '========================
    26.  
    27.  
    28. skipwrite:
    29.  
    30.  
    31.  
    32. Loop
    33. Close #1
    Last edited by iqchicken; Jan 7th, 2005 at 01:09 AM.
    -----------------------------------------------
    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?

    Try something like this.
    VB Code:
    1. Dim arrPIs() As String
    2. ReDim arrPIs(0)
    3. Dim numIPs As Long
    4. Dim Found As Boolean
    5.  
    6. Do Until EOF(1)
    7.     Line Input #1, strNewLine
    8.     ' get the ip string from the start of the log
    9.     strIP = (Mid(strNewLine, 1, 15))
    10. '============================================== PROBLEM
    11.     'loop through the array to see if the ip is in it
    12.     Found = False
    13.     For i = 0 To numIps
    14.         If strIP = arrIPs(i) Then
    15.             Found = True
    16.             Exit For
    17.         End If
    18.     Next i
    19.  
    20.     If Found = False Then
    21.         arrIPs(numIPs) = strIP
    22.         numIps = numIPs + 1
    23.         ReDim Preserve arrIPs(numIPs)
    24.         txtSimple = txtSimple & strIP & strwrap
    25.     End If        
    26. '========================
    27.  
    28. Loop
    29. Close #1
    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?

    I always set NumIPs to 0 right after declaring. It's an old habit that has saved me quite a few times. It will work this way, but only if the variable is declared and destroyed. For instance, if you need to know NumIPs somewhere else, you can't use it. If you make it a public variable, then you NEED to set it to 0 before you start the banning routine, or things will go awry quickly, and it will be very hard to figure out why.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Re: Simple code.. What's wrong?

    hmm... that code looks really promising.. thanks!!! I can't wait to try it out.

    I don't know why I don't just download a log parser.. but... i like to make things myself..
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Simple code.. What's wrong?

    Just wanted to mention that you don't have to declare and use your own "strwrap" since VB has done that already. Either use vbCrLf or vbNewLine.

    Also adding text to the end of a text box is much faster then replacing all text:
    VB Code:
    1. txtSimple.SelStart = Len(txtSimple.Text)
    2. txtSimple.SelText = strIP & vbCrLf

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

    Re: Simple code.. What's wrong?

    Quote Originally Posted by dglienna
    I always set NumIPs to 0 right after declaring. It's an old habit that has saved me quite a few times. If you make it a public variable, then you NEED to set it to 0 before you start the banning routine, or things will go awry quickly, and it will be very hard to figure out why.
    It's not necessary to initialise numIPs if it's private to the routine, which I suggest. You are quite correct about 'things going awry with public variables', that's why it's a good idea to use them sparingly. IMHO there are better (and safer) ways to pass and share data between forms/modules etc.
    Pete

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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Re: Simple code.. What's wrong?

    hmm.. thanks for the tips guys. I've been working on vb for a while now.. and I'll never know enough about it... is there any point where i become a professional?
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

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

    Re: Simple code.. What's wrong?

    Just keep pluggin away.... And don't be afraid to ask questions. You'll get there.

    Good luck
    Pete

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

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    16

    Re: Simple code.. What's wrong?

    Sweet. Thanks for the encouragement.. I can use it for those days when i want to give it all up..

    P.S. I've been to Fremantle (fermantle?) and Perth.. love it.. can't wait to go back.
    -----------------------------------------------
    I am incapable of advanced reasoning.
    -----------------------------------------------

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

    Re: Simple code.. What's wrong?

    You are a professional when you have been paid to do some work, and properly initiated when you fixed a problem..

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