Useful Subroutines

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
Posts: 7
Joined: Wed Nov 28, 2018 7:35 pm

Useful Subroutines

Post by nikomaru »

I'd like to continue the usefulness of the main pages' tutorials and maintain a thread that provides useful, simple, and modular routines that are absent from the tutorials. I run qbx 7.1, but I don't use extra libraries, kinda as a rule. Why, exactly? Think of it as a programming challenge. I grew up with qb 1.1. I didn't know they'd made any more past that when I started fiddling with C, C++, js, darkBasic, python, lua, and a few others. Recently saw the trend for tiny games on tiny emulators and thought "Hey, that's perfect for QB!" 640x480 resolution, 13 screen modes, 256 colors at a time, 64K RAM access maximum. I love it! Then I saw all the adjustments people have done to extend QB and I thought, "Well, why don't I just use LUA and some graphics libraries, if people are going to 'update' QB to keep it relevant. LOVE is a great program and it makes good use of LUA, but I love my QB. It may not be a graphics powerhouse, and it may be slow to process strings and arrays, but by golly I know how to do what I want to do!

Sorry for the rant.

I'd like to note also, this thread should be aimed at beginners who are figuring out their way around the relatively easy standard library of QBASIC. So we should avoid using functions that require loading extra libraries for advanced functions like ABSOLUTE.

I know I was confused. Trying to run a program that had CALL ABSOLUTE or CALL INTERRUPT in it and not knowing why the subprogram wasn't available - so frustrating.

Anyway, I'd like to start of with a simple sentence parser that I think is as straightforward as they come.

Note: 'words' is an array you'll want to DIM in the main program. I set the array size to 32.

Code: Select all

' *******
' parse the input: separate words
' put words in separate array elements
' count it up
' *******
  ' parsing z, super easy
  zLen = LEN(z)				' initial value, for sorting through z-string
  r = 0					' initializing value, for sanity
  maxWord = 31				' limit to match words() size

  FOR a = 1 TO zLen			' here we use the length of input string 'z'
    inClient$ = MID$(z, a, 1)		' MID$ magically grabs 1 letter at time and copies it to inClient$

    SELECT CASE inClient$		' select case uses the single letter to look for something
      CASE CHR$(33) TO CHR$(173)	' printable characters
        words(r) = words(r) + inClient$	' add printable character to the array at 'r'

      CASE CHR$(32)                 	' space = new word
        r = r + 1			' increment array location
        IF r > maxWord THEN r = maxWord	' keeps words() from throwing error


  NEXT a				' going through the list up to zLen
Writing a sentence longer than 32 words will require some doing.

So, all the words from the sentence are now separated into an array and numbered 0 - xx, where xx is the number of words in the sentence. Ha! You might think to pass 'r' out of this sub somewhere so you can use it later if you want (if you're unsure, sometimes setting a global variable in the main program will work in a snap, but most people will suggest passing BYVAL the 'r' back to another variable). For what?

Well, let's example a saying that you want to compare two sentences, and you've already split them down into separate arrays. QBASIC doesn't have a good way to show the number of elements in an array, so saving this number somewhere will give you a head start. Then when you want to compare the values in two arrays, you can use this number, compare the two array lengths, and not exceed the shorter one which will throw an error.

But that's not what we're doing here. Here you've got a straightforward parser. If you want to parse other sentence elements specifically, then add a new CASE item and do whatever you want.

Enjoy! And let's show Pete some appreciation for keeping the forums running and the website up as a resource to all newcomers!

Thanks, Pete!

Post Reply