Results 1 to 9 of 9

Thread: re-ordering array indices not working; is there a better way?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    re-ordering array indices not working; is there a better way?

    This VB6 code is supposed to re-order the elements in array 'EngRusArr' and copy them to array 'RusEngArr'
    The math behind how the elements are swapped is simple; every third element 'jumps' back 2 positions, and the previous 2 move forward to fill the 'gaps' so to speak. That bit works, but...

    ReDim RusEngArr(1 To UBound(EngRusArr))
    For orig = 1 To (UBound(EngRusArr) + 0)
    indx = orig / 3
    If indx = Int(indx) Then swap = orig - 2
    If indx <> Int(indx) Then swap = orig + 1
    ReDim RusEngArr(swap)
    RusEngArr(swap) = EngRusArr(orig)
    Next orig

    For p = LBound(RusEngArr) To UBound(RusEngArr)
    InkEdit6.Text = InkEdit6.Text & vbNewLine & RusEngArr(p)
    Next p



    ...In the second loop, all the array elements are empty. All the information that was in the original array hasn't stayed in the new one. Is there something wrong with my 2nd redim statement?
    Is there a simpler/better/functional way to do this? Simply using the existing 'If' statemnts to 'sort' the original array, and then make it equivalent to the new array, would be fine. I just can't work out how to do this from what I've seen of 'sorting' code.
    Last edited by moorea21; Feb 4th, 2016 at 07:50 AM.

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: re-ordering array indices not working; is there a better way?

    you are ReDimensioning your array every iteration through the loop...

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: re-ordering array indices not working; is there a better way?

    Ah... I see. I was hoping just to redim that one element. It sort of works when I comment out the offending line, but at the end of the 1st loop it tells me 'subscript out of range', referring to RusEngArr(swap.) I havent been able to get around that, yet.

    Thanks for pointing out the mistake.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: re-ordering array indices not working; is there a better way?

    You don't redim an element... you redim an array...

    you don't even need the second array... what you're doing is actually a fairly basic swap, it just happens to involve three elements moving and not just two...

    I'm shooting from the hip here as I don't have VB6 installed ... but I think something like this would be easier to deal with:

    Code:
    Dim elem1 as yourtype
    Dim elem2 as yourtype
    
    For orig = 1 To (UBound(EngRusArr) + 0) Step 3 ' we're dealing with every third element
      elem1 = EngRusArr(orig)
      elem2 = EngRusArr(orig + 1)
      EngRusArr(orig) = EngRusArr(orig + 2)
      EngRusArr(orig + 1) = elem1
      EngRusArr(orig + 2) = elem2
    Next
    
    For p = LBound(EngRusArr) To UBound(EngRusArr)
    InkEdit6.Text = InkEdit6.Text & vbNewLine & EngRusArr(p)
    Next p

    Unless you acvtually do need them in a different array... but stillll
    Code:
    Dim elem1 as yourtype
    Dim elem2 as yourtype
    ReDim RusEngArr(1 To UBound(EngRusArr))
    
    For orig = 1 To (UBound(EngRusArr) + 0) Step 3 ' we're dealing with every third element
      elem1 = EngRusArr(orig)
      elem2 = EngRusArr(orig + 1)
      RusEngArr(orig) = EngRusArr(orig + 2)
      RusEngArr(orig + 1) = elem1
      RusEngArr(orig + 2) = elem2
    Next
    
    For p = LBound(RusEngArr) To UBound(RusEngArr)
    InkEdit6.Text = InkEdit6.Text & vbNewLine & RusEngArr(p)
    Next p
    and I think that can even be done with one elem in the swap:
    Code:
    Dim elem1 as yourtype
    ReDim RusEngArr(1 To UBound(EngRusArr))
    
    For orig = 1 To (UBound(EngRusArr) + 0) Step 3 ' we're dealing with every third element
      elem1 = EngRusArr(orig + 2)
      RusEngArr(orig + 2) = EngRusArr(orig + 1)
      RusEngArr(orig + 1 = EngRusArr(orig)
      RusEngArr(orig) = elem1
    Next
    
    For p = LBound(RusEngArr) To UBound(RusEngArr)
    InkEdit6.Text = InkEdit6.Text & vbNewLine & RusEngArr(p)
    Next p
    Uhhh... yeah, use that last one... I realized the others moved the elements in the wrong direction... the last one has it right.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: re-ordering array indices not working; is there a better way?

    You should step through your array 3 elements at a time.

    Depending on how many elements are in the array, your algorithm will only work if the array size is a multiple of 3, so you will have to handle any remaining elements after the main loop finishes.

    ah tg. I was trying not to do his homework for him

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: re-ordering array indices not working; is there a better way?

    DEX - yeah... normally I wouldn't either... but the code he had was so convoluted that I'm not sure I could have done it justice by telling him how to fix it... plus I was bored while waiting for a conf call to begin.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: re-ordering array indices not working; is there a better way?

    Thanks all, I'll use a more direct method then, something only using the one array seems a good idea.

  8. #8
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: re-ordering array indices not working; is there a better way?

    if i understood correctly, this is for translating from one language to the other
    so why are 3 array entrees needed to translate from one language to the other
    maybe could you give the first 3 entrees in the array
    do not put off till tomorrow what you can put off forever

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: re-ordering array indices not working; is there a better way?

    It's sorted now, thanks for the interest. You are correct, its part of a translation/reading tool for Russian texts for an English speaker (me). The third element is some xml formatting, as the final document is saved as html. I can let you know more if youre still curious, of course.

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