Results 1 to 5 of 5

Thread: [RESOLVED] Sorting array stuff

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Sorting array stuff

    Say i had an array of IP addresses:

    IP_array(0)="192.168.1.1"
    IP_array(1)="192.168.1.1"
    IP_array(2)="192.168.1.1"
    IP_array(3)="192.168.1.2"
    IP_array(4)="192.168.1.2"
    IP_array(5)="192.168.1.1"
    IP_array(6)="192.168.1.1"
    IP_array(7)="192.168.1.3"
    IP_array(8)="192.168.1.1"

    How could i place these into a different array that each IP only shows up once:

    Sorted_IP_array(0)="192.168.1.1"
    Sorted_IP_array(1)="192.168.1.2"
    Sorted_IP_array(2)="192.168.1.3"

    and then in another array it says how many times each array showed up:

    IP_Frequency_array(0)="6" 'Index links to 192.168.1.1 in the above array
    IP_Frequency_array(1)="2"
    IP_Frequency_array(2)="1"


    I'm creating a program that gets Apache logs and analyzes them and i need to get all the different IP addresses and files & see how many times an IP accesses an indvidual file, how many times an IP address accessed any file, and how many times a file was accessed. I can do the rest once i get this bit done, but i can't get my head around it.

    Thanks for your help.

  2. #2
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: Sorting array stuff

    Shooting from the hip here, but perhaps set up a collection object and add the IP addresses to it, using the IP address as the key value? By using On Error, you can catch how many times this fails because the key value is duplicated in the collection...?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Sorting array stuff

    I have no idea how to do that =/ Do you have an example?

  4. #4
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: Sorting array stuff

    ...not the neatest code you've ever seen, nor the most efficient, but proves the concept:

    Code:
    Option Explicit
    Dim IP_Array(9) As String
    Dim IP_Freq() As Long
    
    Private Sub Form_Load()
    
    IP_Array(0) = "192.168.1.1"
    IP_Array(1) = "192.168.1.1"
    IP_Array(2) = "192.168.1.1"
    IP_Array(3) = "192.168.1.2"
    IP_Array(4) = "192.168.1.2"
    IP_Array(5) = "192.168.1.1"
    IP_Array(6) = "192.168.1.1"
    IP_Array(7) = "192.168.1.3"
    IP_Array(8) = "192.168.1.1"
    
    Call GetUniqueColValues
    
    End Sub
    
    Private Sub GetUniqueColValues()
    
        Dim x As Long
        Dim y As Long
        Dim col As Collection
        
        Set col = New Collection
        For x = 0 To 8
            On Error Resume Next
            col.Add IP_Array(x), IP_Array(x)
        Next
        '---------------------------------------------------------------------
        '-- At this point, col contains 3 items, 1 for each of the unique values
        '-- in the original IP_Array() array, and col.Count = 3
        '-- Now we can count how many times each unique item occurs and place the
        '-- reasults in the IP_Freq array
        '---------------------------------------------------------------------
        ReDim IP_Freq(col.Count) As Long
        For x = 0 To 8
            For y = 1 To col.Count
                If col.Item(y) = IP_Array(x) Then
                    IP_Freq(y) = IP_Freq(y) + 1
                    Exit For
                End If
            Next
        Next
        '---------------------------------------------------------------------
        '-- Prove this example works
        '---------------------------------------------------------------------
        For y = 1 To col.Count
            Debug.Print col.Item(y), IP_Freq(y)
        Next
        '---------------------------------------------------------------------
    
    End Sub
    The output from the above program is as follows:

    192.168.1.1 6
    192.168.1.2 2
    192.168.1.3 1

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Sorting array stuff

    Thanks man, let me try this and i will get back to you very soon.

    Yep Dude! Works great!!!

    Changed a little so it could handle massive amounts of IPs, but works like a charm & it's fast.
    Last edited by Slyke; Apr 25th, 2007 at 03:34 PM.

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