Results 1 to 2 of 2

Thread: [RESOLVED] evaluate and note/save array duplicates

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    2

    Resolved [RESOLVED] evaluate and note/save array duplicates

    Hi, I have 4 arrays one holding which items in another array were duplicates, with those indexes I have other arrays to index, coordinates and values. Some of the coordinates are equal, so I have to take the coordinate with the highest value and drop the other one (or two, or three, etc). I am having a really tough time with this, which I thought would be easy. Any ideas will be VERY appreciated!

    Code:
    Sub testLogic()
    
        Dim index(13) As Variant
        Dim coord(13) As Variant
        Dim dups(13) As Variant
        Dim tVAL(13) As Variant
    
        Dim i As Long
    
        Dim rIndex() As Variant
        Dim rCoord() As Variant
        Dim rDups() As Variant
        Dim rtVal() As Variant
        
        index(0) = "0"
        index(1) = "1"
        index(2) = "2"
        index(3) = "3"
        index(4) = "4"
        index(5) = "5"
        index(6) = "6"
        index(7) = "7"
        index(8) = "8"
        index(9) = "9"
        index(10) = "10"
        index(11) = "11"
        index(12) = "12"
        index(13) = "13"
        
        coord(0) = "1,57"
        coord(1) = "1,57"
        coord(2) = "1,57"
        coord(3) = "1,58"
        coord(4) = "1,59"
        coord(5) = "1,60"
        coord(6) = "1,61"
        coord(7) = "1,61"
        coord(8) = "1,62"
        coord(9) = "1,63"
        coord(10) = "1,64"
        coord(11) = "1,65"
        coord(12) = "1,65"
        coord(13) = "1,65"
        
        dups(0) = "x"
        dups(1) = "x"
        dups(2) = "x"
        dups(3) = ""
        dups(4) = ""
        dups(5) = ""
        dups(6) = "x"
        dups(7) = "x"
        dups(8) = ""
        dups(9) = ""
        dups(10) = ""
        dups(11) = "x"
        dups(12) = "x"
        dups(13) = "x"
         
        tVAL(0) = "5"
        tVAL(1) = "5"
        tVAL(2) = "4.5"
        tVAL(3) = "5"
        tVAL(4) = "5"
        tVAL(5) = "5"
        tVAL(6) = "3"
        tVAL(7) = "4.5"
        tVAL(8) = "5"
        tVAL(9) = "5"
        tVAL(10) = "4.5"
        tVAL(11) = "3.4"
        tVAL(12) = "5"
        tVAL(13) = "5"
    
        For i = 0 To UBound(dups) ' LOOP THROUGH TO SAVE A SUBSET OF DUPS STORED IN rDups
            If dups(i) = "x" Then
                If IsBounded(rDups) Then
                    ReDim Preserve rDups(0 To UBound(rDups) + 1)
                    rDups(UBound(rDups)) = i
                Else
                    ReDim Preserve rDups(0)
                    rDups(0) = i
                End If
            End If
        Next i
    
        For i = 0 To UBound(rDups) ' LOOP THROUGH DUPS TO GET ALL DUP COORDS STORED IN rCoord
            If IsBounded(rCoord) Then
                    ReDim Preserve rCoord(0 To UBound(rCoord) + 1)
                    rCoord(UBound(rCoord)) = coord(CLng(rDups(i)))
                Else
                    ReDim Preserve rCoord(0)
                    rCoord(0) = coord(CLng(rDups(i)))
                End If
        Next i
        
        For i = 0 To UBound(rDups) ' LOOP THROUGH DUPS TO GET ALL DUP'S tVALS STORED IN rtVAL
                If IsBounded(rtVal) Then
                    ReDim Preserve rtVal(0 To UBound(rtVal) + 1)
                    rtVal(UBound(rtVal)) = tVAL(CLng(rDups(i)))
                Else
                    ReDim Preserve rtVal(0)
                    rtVal(0) = tVAL(CLng(rDups(i)))
                End If
        Next i
        
    	' NOW WALK THROUGH COORDINATES rCoord AND FOR EVERY COORD THAT IS EQUAL, STORE THE INDEX NUMBER OF THE ONE WITH THE HIGHER rtVAL
    	' EITHER ONE IF THEY ARE THE SAME, SO FROM THE DATA ABOVE I SHOULD BE STORING THESE INDEX VALUES:
    	' 0 (or 1)
    	' 7
    	' 12 (or 13)
    
    End Sub

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    2

    Re: evaluate and note/save array duplicates

    I figured it out. You know I had sorted the data by the X and Y coordinates, to group the duplicates, prior to running everything, but it never occurred to me to add a level to the sort and then sort by values descending! Then all I had to do was step through in my code and grab the first or each group. That way I get one even if they're the same AND the first one is also always going to be the highest!

    D-UH! I can't believe I spent so many hours trying to figure out how to look ahead and behind an indeterminate number of members, testing and re-saving in other variables, etc. Man my code was becoming more of a mess than usual! Here's the mess of a function I ended up using, which did the trick:
    vb Code:
    1. Function markDupsForRemoval(index() As Variant, coord() As Variant, dups() As Variant, daq() As Variant) As Variant ' RETURNS A LIST OF INDEXES/ROWS TO BE SAVED
    2.     Dim i As Long
    3.     Dim j As Long
    4.     Dim n As Long
    5.     Dim resIndex() As Variant
    6.     Dim resDups() As Variant
    7.     Dim resCoords() As Variant
    8.     Dim resDAQ() As Variant
    9.  
    10.     For i = 0 To UBound(index) ' LOOP THROUGH TO SAVE A SUBSET OF DUPS
    11.         If dups(i) = "x" Then
    12.             If IsBounded(resDups) Then
    13.                 ReDim Preserve resDups(0 To UBound(resDups) + 1)
    14.                 resDups(UBound(resDups)) = i
    15.             Else
    16.                 ReDim Preserve resDups(0)
    17.                 resDups(0) = i
    18.             End If
    19.         End If
    20.     Next i
    21.    
    22.     'For i = 0 To UBound(resDups)
    23.     '    MsgBox "resDup " & i & " = " & resDups(i)
    24.     'Next i
    25.  
    26.     For i = 0 To UBound(resDups) ' LOOP THROUGH DUPS TO GET ALL DUP COORDS
    27.         If IsBounded(resCoords) Then
    28.                 ReDim Preserve resCoords(0 To UBound(resCoords) + 1)
    29.                 resCoords(UBound(resCoords)) = coord(CLng(resDups(i)))
    30.             Else
    31.                 ReDim Preserve resCoords(0)
    32.                 resCoords(0) = coord(CLng(resDups(i)))
    33.             End If
    34.     Next i
    35.  
    36.     For i = 0 To UBound(resDups) ' LOOP THROUGH DUPS TO GET ALL DUP'S DAQS
    37.             If IsBounded(resDAQ) Then
    38.                 ReDim Preserve resDAQ(0 To UBound(resDAQ) + 1)
    39.                 resDAQ(UBound(resDAQ)) = daq(CLng(resDups(i)))
    40.             Else
    41.                 ReDim Preserve resDAQ(0)
    42.                 resDAQ(0) = daq(CLng(resDups(i)))
    43.             End If
    44.     Next i
    45.  
    46.     Dim arrHI() As Variant
    47.     Dim jlast As Long
    48.     Dim ckCoord As Variant
    49.     Dim bIn As Boolean
    50.     j = 0
    51.     bIn = False
    52.    
    53.     ckCoord = resCoords(0)
    54.     For i = 0 To UBound(resCoords) ' LOOP THROUGH DUPS TO GET ALL DUP'S DAQS
    55.         If resCoords(i) = ckCoord Then ' SAME COORD GRAB FIRST DAQ VALUE INDEX, SINCE SORTED DESCENDING
    56.             If j = 0 Then
    57.                 If Not bIn Then
    58.                     ReDim Preserve arrHI(j)
    59.                     arrHI(j) = resDups(i)
    60.                     j = j + 1
    61.                     bIn = True
    62.                 End If
    63.             Else
    64.                 If Not bIn Then
    65.                     ReDim Preserve arrHI(0 To UBound(arrHI) + 1)
    66.                     arrHI(j) = resDups(i - 1)
    67.                     j = j + 1
    68.                     bIn = True
    69.                 End If
    70.             End If
    71.         Else ' DIFFERENT COORDS UPDATE CHECK VALUE
    72.             ckCoord = resCoords(i)
    73.             bIn = False
    74.         End If
    75.     Next i
    76.    
    77.     For i = LBound(index) To UBound(index)
    78.        
    79.     Next i
    80.  
    81.     markDupsForRemoval = arrHI
    82.    
    83.     Erase resDups
    84.     Erase resCoords
    85.     Erase resDAQ
    86.  
    87. End Function

Tags for this Thread

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