Results 1 to 8 of 8

Thread: Truncate the Array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Truncate the Array

    Hello:

    I have read about 47 posts on this today. Nothing makes sense.

    I gave the following 2d array (110,2). I have been able to sort it, but I would just like to truncate it for the blank values.

    Thoughts...

    Code:
    1: 3: DJK
    9: 11: DJK
    2: 4: DLS
    3: 5: DLS
    7: 9: DLS
    41: 43: EJJ
    42: 44: EJJ
    50: 52: EJJ
    51: 53: EJJ
    53: 55: EJJ
    63: 65: EJJ
    21: 23: JRD
    23: 25: JRD
    31: 33: JRD
    88: 90: JRD
    89: 91: JRD
    93: 95: JRD
    11: 13: NBS
    6: 8: NMS
    8: 10: NMS
    19: 21: PLL
    29: 31: PLL
    30: 32: PLL
    36: 38: PLL
    87: 89: PLL
    39: 41: RME
    40: 42: RME
    35: 37: SEO
    14: 16: SRM
    15: 17: SRM
    16: 18: SRM
    17: 19: SRM
    56: 58: SRM
    80: 82: SRM
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : : 
    : :
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Truncate the Array

    Reconsider how you are dimensioning it. You can only resize a multidimensional array on its last dimension while using the Preserve keyword.

    Dim myarray (110,2) -- won't work for you
    Dim myarray (2,110) -- will work for you: ReDim Preserve myarray (2, 77) for example
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: Truncate the Array

    110 rows and 3 columns. I'm not sure I can easily restructure things.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Truncate the Array

    The first dimension is columns, the second rows, the 3rd if any is planes, the 4th cubes, etc.

  5. #5
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Truncate the Array

    it seems from the log that 2 values are numbers and the other is a 3 letter string.
    if thats so, I would make a custom type,
    example:

    type mytype
    value1 as integer
    value2 as integer
    myletter as string * 3
    end type

    Dim myarray(110) as mytype

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Truncate the Array

    Quote Originally Posted by ssabc View Post
    110 rows and 3 columns. I'm not sure I can easily restructure things.
    Either restucture or create a new array and transfer old array items to new in a loop, after first sizing the new array.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

    Re: Truncate the Array

    Quote Originally Posted by LaVolpe View Post
    Either restucture or create a new array and transfer old array items to new in a loop, after first sizing the new array.
    Or Dim the new Array to its new Size, and then CopyMemory the VarPtr of the old Array to the new Array with the correct/new length.
    Don't forget to cheat the ref-count.

    EDIT: I'm with baka on that re using an UDT.
    But that opens another can of worms, since the OP said something about sorting.

    EDIT2: A "Hacker's" Approach might be to manipulate the SAFEARRAY2D-Descriptor, since that one has a Field called Bounds Of Type SAFEARRAYBOUND....
    No Idea if that approach might start a nuclear rocket in North Korea.....
    Last edited by Zvoni; Oct 23rd, 2020 at 01:10 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Truncate the Array

    sorting is easy. u can modify a quicksort,
    example:

    Code:
    Private Sub QuickSort(c() As mytype, ByVal first As Long, ByVal Last As Long)
        Dim Low As Long, High As Long
        Dim MidValue As String * 3
        Dim Tmp as mytype
    
        Low = first
        High = Last
        MidValue = c((first + Last) \ 2).myletter
            
        Do
            While c(Low).myletter < MidValue
                Low = Low + 1
            Wend
                
            While c(High).myletter  > MidValue
                High = High - 1
            Wend
                
            If Low <= High Then
                Tmp = c(Low): c(Low) = c(High): c(High) = Tmp
                Low = Low + 1
                High = High - 1
            End If
        Loop While Low <= High
            
        If first < High Then QuickSort c, first, High
        If Low < Last Then QuickSort c, Low, Last
    End Sub
    and u can have 3 different, if u need to sort all 3 values.
    if u need to sort both 1&2 together it will be more complicated.

    array resizing, I would use Redim if its about resizing the bounds,
    if its instead removing 1 item I would use a replace method.
    (I always use Redim myarray(1 to 110) instead of Redim myarray(110) this because I see 0 as nothing + I always have an extra amount value that tells me if the array is empty or not)
    so here:

    Code:
    Private Sub RemoveMyArray(ByVal c&)
        Dim i&
            If c < myarrays Then
                For i = c To myarrays  - 1
                    myarray(i) = myarray(i + 1)
                Next i
            End If
            myarrays = myarrays  - 1
            If myarrays = 0 Then Erase myarray Else ReDim Preserve myarray(1 To myarrays)
    End Sub

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