-
Feb 4th, 2016, 07:44 AM
#1
Thread Starter
Member
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.
-
Feb 4th, 2016, 09:04 AM
#2
Re: re-ordering array indices not working; is there a better way?
you are ReDimensioning your array every iteration through the loop...
-
Feb 4th, 2016, 09:24 AM
#3
Thread Starter
Member
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.
-
Feb 4th, 2016, 09:44 AM
#4
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
-
Feb 4th, 2016, 09:46 AM
#5
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
-
Feb 4th, 2016, 10:17 AM
#6
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
-
Feb 4th, 2016, 01:52 PM
#7
Thread Starter
Member
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.
-
Feb 4th, 2016, 03:27 PM
#8
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
-
Feb 4th, 2016, 05:10 PM
#9
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|