Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Simplifying... (Arrays and Mod help)


Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2004, 10:08 PM
spamonkey8's Avatar
spamonkey8 spamonkey8 is offline
Junior Contributor
 
Join Date: Apr 2003
Location: Between Your Ears
Posts: 337
Default 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
Reply With Quote
  #2  
Old 01-16-2004, 10:30 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

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.
Reply With Quote
  #3  
Old 01-16-2004, 10:41 PM
spamonkey8's Avatar
spamonkey8 spamonkey8 is offline
Junior Contributor
 
Join Date: Apr 2003
Location: Between Your Ears
Posts: 337
Default

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
Reply With Quote
  #4  
Old 01-16-2004, 10:52 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

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.
Reply With Quote
  #5  
Old 01-16-2004, 11:12 PM
spamonkey8's Avatar
spamonkey8 spamonkey8 is offline
Junior Contributor
 
Join Date: Apr 2003
Location: Between Your Ears
Posts: 337
Default

Thank you!
__________________
[vb] and [/vb] tags make the world go 'round

Last edited by spamonkey8; 01-16-2004 at 11:32 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->