 |
 |

01-16-2004, 10:08 PM
|
 |
Junior Contributor
|
|
Join Date: Apr 2003
Location: Between Your Ears
Posts: 337
|
|
Simplifying... (Arrays and Mod help)
|
I have a little bit of code that I'd like to simplify in a project about permutations. It's a loop that, i'm sure, can be replaced by just the right Mod statement. There are two arrays, one large and one much smaller. What the loop does is for every byte in the larger array, the smaller is incremented one. When the position reaches the upper boundary of the smaller array, it goes back to zero. This is it:
Code:
For i = 1 To UBound(bytLarge())
If lPosition = UBound(bytSmall()) Then
lPosition = 0
Else
lPosition = lPosition + 1
End If
Next
'I messed around with it and I found a statement that worked for many
'array dimensions, but it fails for some values, such as if the upper
'bounds are 41 and 5 for bytLarge and bytSmall, respectively
lPosition = ((UBound(bytLarge()) + 1) Mod (UBound(bytSmall()) + 1)) - 1
Thanks everybody 
|
__________________
[vb] and [/vb] tags make the world go 'round
|

01-16-2004, 10:30 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
If the array will start at 0 then this should work
Code:
For i = 1 to UBound(bytLarge)
IPosition = (IPosition + 1) Mod UBound(bytSmall)
Next
But I prefer to not repeatedly call functions if I can help it so I would
probably use
Code:
Dim L as long
L = UBound(bytSmall)
For i = 1 to UBound(bytLarge)
IPosition = (IPosition + 1) Mod L
Next
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

01-16-2004, 10:41 PM
|
 |
Junior Contributor
|
|
Join Date: Apr 2003
Location: Between Your Ears
Posts: 337
|
|
Wait, there's no way to use a single Mod call to do this? It's just a simple remainder problem, I don't see why I'd have to still loop.

|
__________________
[vb] and [/vb] tags make the world go 'round
|

01-16-2004, 10:52 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
I guess I don't understand what you're trying to do. ( I didn't quite
understand it the first time, but from your code, I thought my code
simplified what it was doing).
So  , what is it you're trying to do?
If you only want to get the same value for lposition with a mod instruction,
as you do when you run it through that loop and exit, then I guess you want
lposition = Ubound(bytLarge) Mod (UBound(bytSmall) + 1)
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Last edited by passel; 01-16-2004 at 11:15 PM.
|

01-16-2004, 11:12 PM
|
 |
Junior Contributor
|
|
Join Date: Apr 2003
Location: Between Your Ears
Posts: 337
|
|
__________________
[vb] and [/vb] tags make the world go 'round
Last edited by spamonkey8; 01-16-2004 at 11:32 PM.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|