Results 1 to 9 of 9

Thread: RESOLVED * How to do this the most efficient way

  1. #1

    Thread Starter
    Hyperactive Member mvrp350's Avatar
    Join Date
    Feb 2001
    Location
    Best, the Netherlands
    Posts
    322

    RESOLVED * How to do this the most efficient way

    Hi,

    What I have is a list with x,y,z coordinates (I.e. x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4.. etc)

    I split the list, and put the X, Y, Z coordinates in three seperate arrays: arListX, arListY, arListZ.

    The problem is that some coordinates have more then one occurence in the list with coordinates. (x1,y1,z1) = (x3,y3,z3)

    What I need to do is to filter out all the double occurences in my list.
    And after that I need to have the list sorted.
    The sorting has to be done in the order x,y,z

    x1 = 50 y1 = 12 z1 = 25
    x2 = 75 y2 = 12 z2 = 25
    x3 = 50 y3 = 10 z3 = 25
    x4 = 60 y4 = 12 z4 = 37
    x5 = 30 y5 = 10 z5 = 37
    x6 = 60 y6 = 10 z6 = 37
    x7 = 60 y7 = 10 z7 = 25
    x8 = 60 y8 = 12 z8 = 25


    this should become:
    x1 = 30 y1 = 10 z1 = 37
    x2 = 50 y2 = 12 z2 = 25
    x3 = 60 y3 = 10 z3 = 25
    x4 = 60 y4 = 10 z4 = 37
    x5 = 60 y5 = 12 z5 = 25
    x6 = 60 y6 = 12 z6 = 37
    x7 = 75 y7 = 12 z7 = 25

    Thanks anyway,
    mvrp350
    Last edited by mvrp350; Jul 21st, 2004 at 07:11 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I would recommend sorting them first, that way any duplicates will be adjacent to each other (and will only require a fairly simple loop).

    There are many examples on this forum of sorting, try searching for BubbleSort or QuickSort.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Putting them into three arrays is not helping - it's breaking the "connection" between the values.

    Try making an array that is dim'd like COORD(1 to 100,1 to 3)

    COORD(1,1) gets the x1 value
    COORD(1,2) gets the y1 value
    COORD(1,3) gets the z1 value

    COORD(2,1) gets the x2 value
    COORD(2,2) gets the y2 value
    COORD(2,3) gets the z2 value

    Before you put a x,y,z combo into the COORD array, loop through all the existing items and see if it's in the array already.

    This takes care of dup's - not sorting.

    To SORT, since you are looping through the array to find a dup, I would remember what array spot held the first x,y,z that was "greater" than the x,y,z I'm about to put.

    Before putting the new x,y,z into the array, I would "open" that held spot, by moving all the items from that spot up one.

    SORTING after filling is not needed here, since filling requires dup checking anyway.

    There are other ways to do this with collections, etc - but this would be a standard "array" method to use for this action.

  4. #4
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    Something like this to find duplicates,

    VB Code:
    1. Private Const strtemp   As String = _
    2. "z2,y2,x1,y1,x1,z1,x2,y2,z2,z2,y3,x3,y3,z3,y3"
    3. Private Sub Command1_Click()
    4.  
    5.     myarray = Split(strtemp, ",")
    6. nextval:
    7.     If j > UBound(myarray) Then
    8.         Exit Sub
    9.     End If
    10.     For i = LBound(myarray) To UBound(myarray)
    11.         If myarray(i) = myarray(j) Then
    12.             If i <> j Then
    13.               '  If i > j Then
    14.                     myarray(i) = vbNullString
    15.                    
    16.                     Next k
    17.                ' End If
    18.             End If
    19.         End If
    20.     Next i
    21.     j = j + 1
    22.     GoTo nextval
    23.  
    24. End Sub

    Then could maybe loop through and redim the array if any nulls were found.

  5. #5

    Thread Starter
    Hyperactive Member mvrp350's Avatar
    Join Date
    Feb 2001
    Location
    Best, the Netherlands
    Posts
    322
    Thanks everybody,

    I think I'm going for the combination of your sugestions:

    1) use split to put the sting in an array
    2) loop through the array to make aones arrXYZ (String)
    3) use removeduplicates(arrXYZ) routine I found in this forum to reomve the duplicate entries
    4) write a custom sorting routine using a new array in the way that szlamany described.

    I do this because this routine will be called very frequently, and the amount of coordinates can go into the 100's.
    Using the removeduplicates routine I don't have to loop, because it's using a collection.

    My thanks to all of you

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    You could also use a class of XYZ and store the info into a collection. Then padd the coordinates with zeroes and use the resulting string as the Key in the collection (eg. XYZ-000-000-000). The collection doesn't allow duplicate keys hence .Add will fail for the duplicates... use On Error handling to continue adding the remaining items.

    That way you would do a loop only once (through the text file).

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Word of caution - collections can be slow.

    Arrays of integers or longs are very, very fast.

    100's of entries is not a lot - 10000's of entries would be.

    But, ultimately, you would be the person that should test different results.

    A modificiation of the suggestion I gave would be to not actually move the items up one, to fit the new one in, but instead use a "pointer array" that gets adjusted/incremented.

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Adding to collections is slow?

  9. #9

    Thread Starter
    Hyperactive Member mvrp350's Avatar
    Join Date
    Feb 2001
    Location
    Best, the Netherlands
    Posts
    322
    Thanks for the advice,
    I was also looking for a way to use pointer-arrays instead of moveing the items up and down the list.
    I'm using longs as the input-type so that shouldn't be any problem

    Thanks again

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