« Re: Special word extraction tool (archived ) »

Welcome Guest. Please Login or Register.
Jan 20th, 2018, 6:02pm

Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Re: Special word extraction tool (archived )  (Read 444 times)
Richard Russell

member is offline


Homepage PM

Posts: 689
xx Re: Special word extraction tool (archived )
« Thread started on: Apr 6th, 2016, 4:29pm »

on Apr 4th, 2016, 06:23am, michael gallup wrote:
Purpose: allows you to pick a word from a string..

I'm slightly surprised that nobody has asked the question outright: "Why doesn't BBC BASIC already provide this functionality, either natively or in a library?". After all in Liberty BASIC and some other dialects it's a standard function (word$() in the case of LB).

Having now asked the question myself, I'll answer it! It all comes down to efficiency. Consider this piece of Liberty BASIC code:

    t$ = "The quick brown fox jumps over the lazy dog."
    for i = 1 to 9
      print word$(t$, i)

This works fine, but when you think about exactly what is going on under the hood not everything in the garden is rosy. Each time round the loop (except the first) the word$() function has to repeat everything it did on every previous iteration!

So on the second time round, when it is finding the 2nd word in the string, it can only do that by first finding the first word. And on the third time round, it is having to do again everything it did on both the first and second iterations!

By the time it's got to the 9th time round it's had to find the 1st word 9 times, the 2nd word 8 times and the 3rd word 7 times (and so on). This is hopelessly inefficient: just imagine trying to extract every word in an 80,000-word novel this way.

It makes much more sense to split the string into its individual words in one operation, then there is no wasteful repetition. And that's what the function that BBC BASIC does provide (FN_split in the STRINGLIB library) is designed to do.

If for some reason you cannot live without the functionality of LB's word$() there is a way of improving its efficiency. The code listed below splits the entire string into its component parts on the first call, then on subsequent calls it checks whether the same string has been supplied. If it has it doesn't bother to repeat the splitting operation but just returns the word from the previously created array:

      INSTALL @lib$+"stringlib"

      t$ = "The quick brown fox jumps over the lazy dog."
      FOR i% = 1 TO 9
        PRINT FNword$(t$, i%)

      DEF FNword$(a$, n%)
      PRIVATE a$(), p$, c%
      IF p$<>a$ p$ = a$ : c% = FN_split(p$, " ", a$())
      IF n%<1 OR n%>c% THEN = ""
      = a$(n%-1) 

The price you pay for the improved speed is memory usage, because the array must be retained from one call to the next. What's more the array's memory is leaked each time the function is called with a string containing more words than the previous maximum.

« Last Edit: Apr 6th, 2016, 4:54pm by Richard Russell » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls