Need to get values from one line.

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Guest

Need to get values from one line.

Post by Guest » Fri May 06, 2005 7:33 am

Say for example that I have:

Dummy = "part:section:code,code2,code3:line"

I need to get the 6 values as value1, value2, value3, value4, value5, value 6.
Dummy is retrieved from a file so I can't break it up already in 6 values.

thx

grtz

User avatar
Pete
Site Admin
Posts: 882
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Post by Pete » Fri May 06, 2005 10:01 am

Use MID$

http://www.petesqbsite.com/sections/tut ... rial3.html

There are also a lot of other tutorials that cover it. Do a search.

User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post by Levi » Fri May 06, 2005 10:04 am

Well, binary would be an excellent way to go if you wanted to get each value seperatly from the file itself.

For this I'd say (keep in mind this is very general code) do something like this.

Code: Select all

'Set CurrentPos to 1
CurrentPos = 1

'Start and infinite loop, don't worry it's not really infinit, we have an escape below
Do 

   'See of there is a colon in the text From the last known startint point.
   CurrentPos = Instr(CurrentPos,Dummy,":")
  
   'if there is no colon then search for a comma
   If CurrentPos < 1 then
      CurrentPos = Instr(CurrentPos,Dummy,",")

      'If there is no comma, time to escape.
      If CurrentPos < 1 then

         'Exit the loop
         Exit Do

      Else
         'If there is a comma add to the counter for the value array
         counter = counter +1

         'Then put the text value into the value array at index of counter
         value(counter) = mid(Dummy,CurrentPos,Len(Dummy)-CurrentPos)
      End if
   Else
      'Else if there is a colon, same as comma.
      counter = counter +1
      value(counter) = mid(Dummy,CurrentPos,Len(Dummy)-CurrentPos)
   End if
Loop
Now that is extremely general. I'm not sure about all the math. I continually manage to be off by one or more letters when it comes to this sort of thing but you sound far more competant than I so I know you'll have no problem fixing this up. Good luck.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer

User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post by Levi » Fri May 06, 2005 1:12 pm

Actually, you know what? I just thought up another coding example.

Code: Select all

Do 
   counter = counter + 1

   If mid(Dummy,counter,1) = ":" or mid(Dummy,counter,1) = "," then
      ValueIndex= ValueIndex +1
      Value(ValueIndex) = mid(Dummy,counter,len(Dummy)-counter)
   End if
Loop until counterm > len(Dummy)
That might just do the exact same thing. I don't have time to test it but hopefully it'll work.

Oh, and you know what else I just thought of? These codes will only work so long as the values are only seperated by colons and commas and don't contain any themselves. There is a way to prevent that, just it would take a lot more coding.

Actually I once wrote a program in QB that was suppose to count the number of parameters in a method call for a debugger I was working on for the RPGCode language. It was over two pages long. So whether that is what was needed, or whether that was simply me being stupid I don't know. But it worked like a dream. I could have as many parameters that I wanted. Integer or string. And the string could have all of the punctuation points it wanted. Commas and all. It was great. Oh well. Later days.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer

Post Reply