[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/db/driver/mysqli.php on line 264: mysqli_fetch_assoc(): Couldn't fetch mysqli_result
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/db/driver/mysqli.php on line 326: mysqli_free_result(): Couldn't fetch mysqli_result
Pete's QBASIC Site Discuss QBasic, Freebasic, QB64 and more 2019-02-12T23:37:55-05:00 http://petesqbsite.com/phpBB3/app.php/feed/topic/10210 2019-02-12T23:37:55-05:00 2019-02-12T23:37:55-05:00 http://petesqbsite.com/phpBB3/viewtopic.php?p=33448#p33448 <![CDATA[Useful Subroutines]]>
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:

SUB ParseIt (z AS STRING)'' *******' 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    END SELECT  NEXT a' going through the list up to zLen  END SUB
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!

9999

Statistics: Posted by nikomaru — Tue Feb 12, 2019 11:37 pm


]]>