"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
Go Back  Xtreme Visual Basic Talk > > > "Rolling" Right and Left Through A String Of Characters


Reply
 
Thread Tools Display Modes
  #1  
Old 09-20-2009, 02:18 PM
jgbarber65's Avatar
jgbarber65 jgbarber65 is offline
Regular
 
Join Date: Jul 2003
Location: Newark, DE
Posts: 69
Default "Rolling" Right and Left Through A String Of Characters


If I have a string (mystring$="L2XP05QR4M9D" for example) and then I have a integer value (myvalue=22) and I have a character that is in the string (mystart$="Q"). Now I roll through the string to the right 22 times until I stop on "0". Now I save this "0" in a variable. How do I do "roll" to the right like this? It's easy enough if my integer value was 3 and I stop on "M" but "rolling" back to "L" and continuing is the part I'm struggling with. Can't this be done with Mod or something like this? I can do this with GoTos, but I don't want to use GoTos. I would like to be able to "roll" through this string to the left as well.
Reply With Quote
  #2  
Old 09-20-2009, 07:50 PM
vb5prgrmr"Rolling" Right and Left Through A String Of Characters vb5prgrmr is offline
Senior Contributor

* Expert *
 
Join Date: Mar 2009
Posts: 1,087
Default

You are on the right track...

First, need to figure out from which side you need to start from with the remainder (L/R) by using interger division and then modulus...
Code:
If Len(MyString) >= MyValue Then
  'Direction is starting from left
  MyDirection = 0
Else
  TempValue = MyValue / Len(MyString)
  '0 = left, 1 = right
  MyDirection = TempValue Mod 2 'should equal 1 based upon example above
End If
Then you need to figure out how many positions by mod...
Code:
NoPos = MyValue mod Len(MyString)
Next, test direction...
Code:
If MyDirection = 0 Then
  MyChar = Mid(MyString, NoPos, 1)
Else
  MyChar = Mid(MyString, Len(MyString) - NoPos, 1)
End If
Or something like that (above code is untested as on machine without VB6 to test.



Good Luck
Reply With Quote
  #3  
Old 09-21-2009, 08:32 AM
jgbarber65's Avatar
jgbarber65 jgbarber65 is offline
Regular
 
Join Date: Jul 2003
Location: Newark, DE
Posts: 69
Default

OK.... I'm close but it's not working yet... I am concentrating on just rolling through it to the right for now. Here is what I have... I expected it to output the letter "O" but it is not....

Code:
    myString$ = "L2XP05QR4M9D"
    myValue = 22
    myShift = InStr(1, myString$, "Q", vbTextCompare)
    Do While myPos > Len(myString$)
        myPos = myValue Mod Len(myString$)
        myPos = myPos - Len(myString$)
    Loop
    myChar$ = Mid(myString, myPos + myShift, 1)
    MsgBox myChar$
Reply With Quote
  #4  
Old 09-21-2009, 12:26 PM
jgbarber65's Avatar
jgbarber65 jgbarber65 is offline
Regular
 
Join Date: Jul 2003
Location: Newark, DE
Posts: 69
Lightbulb

Woo hoo! I got it!

Code:
    myString$ = "L2XP05QR4M9D"
    myValue = 22
    myShift = InStr(1, myString$, "Q", vbTextCompare)
    myOffset = myValue + myShift
    myPos = myOffset Mod Len(myString$)
    myChar$ = Mid(myString$, myPos, 1)
    MsgBox myChar$
Reply With Quote
  #5  
Old 09-22-2009, 05:05 AM
jgbarber65's Avatar
jgbarber65 jgbarber65 is offline
Regular
 
Join Date: Jul 2003
Location: Newark, DE
Posts: 69
Default

So now I have a slight issue rolling to the left... The code below seems to work if I roll left past the "L" and wrap around past "D". But if I want to roll to the left from "0" just 2 places and land on "X", the value that is output is null. I have some small mistake here. This is actually the code behind a puzzle game I am working on. It will have grachics, etc... but I need to understand how to get this part working first. Rolling left above works in every case.

This does NOT work...
Code:
    myString$ = "L2XP05QR4M9D"
    myValue = 2
    myShift = InStr(1, myString$, "0", vbTextCompare)
    myOffset = myValue - myShift
    myPos = myOffset Mod Len(myString$)
    myChar$ = Mid(myString$, Len(myString$) - myPos, 1)
    MsgBox myChar$
This DOES work...
Code:
    myString$ = "L2XP05QR4M9D"
    myValue = 22
    myShift = InStr(1, myString$, "0", vbTextCompare)
    myOffset = myValue - myShift
    myPos = myOffset Mod Len(myString$)
    myChar$ = Mid(myString$, Len(myString$) - myPos, 1)
    MsgBox myChar$
What do I have wrong in this code?
Thank you.
Reply With Quote
  #6  
Old 09-22-2009, 05:47 AM
Iceplug's Avatar
Iceplug"Rolling" Right and Left Through A String Of Characters Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

The problem comes from myvalue = 2 and myshift being 5
now you have myoffset = 2 - 5 = -3 (you don't want to pass a negative number into the Mod or the Mid$ function.)
I'd recommend adding the length of the string to myValue.
Thus, you'd have
myvalue = 2 + Len(mystring$) 'equals 14
myshift = 5
myoffset = 14 - 5 = 9
Then you mod it and mypos = 9
myChar$ = Mid(myString$, Len(myString$) - myPos, 1)
this will be Mid(myString$, 12 - 9, 1)
and then you'll have the X in the character that you want.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #7  
Old 09-22-2009, 06:48 AM
jgbarber65's Avatar
jgbarber65 jgbarber65 is offline
Regular
 
Join Date: Jul 2003
Location: Newark, DE
Posts: 69
Default

Thank you very much. I will try that.
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
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters "Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
 
"Rolling" Right and Left Through A String Of Characters
"Rolling" Right and Left Through A String Of Characters
 
-->