RetroCoders Community

Other Basic Programming Languages => SpecBAS => Topic started by: ZXDunny on Jul 19, 2023, 04:50 PM

Title: SpecBAS updated to v0.1323 with a new FOR EACH method
Post by: ZXDunny on Jul 19, 2023, 04:50 PM
In the "things that should have been bloody obvious but I missed them" category is a simple way of iterating through a string with FOR EACH.

It was because of this comic - I'm a fan of SMBC and you should be too - https://www.smbc-comics.com/comic/niiiice

Where the main character adds up the value of letters in a word. The supplied "niiiicccee" is an excellent example. While doing this little routine, I figured that I could expand FOR EACH not only so that it iterates through arrays and ranges, but also characters in a string. Hence:

(https://i.ibb.co/svLMm2L/screenshot-1880.png)

As you can see, the FOR loop assigns the value of b$ to each character in a$ in turn, then updates a running total (variable t) with the ascii value (CODE) of the uppercase (UP$) character in b$.

And it works, and gives the somewhat expected result for "niiicccee" :)

I can tell I'll be using this style of FOR-loop a lot in the future!
Title: Re: SpecBAS updated to v0.1323 with a new FOR EACH method
Post by: ZXDunny on Jul 20, 2023, 07:34 AM
Thanks! It's very handy. The code above without FOR EACH would likely be:

10 INPUT "Word? ";a$
20 t=0:
   FOR i=1 TO LEN a$:
      t+=CODE UP$ a$(i)-64:
   NEXT i:
   PRINT t

So not too different but you don't have to add the length of a$ to the loop, nor address each character in turn via the array-style slicer. It's slightly easier to understand what's going on with FOP EACH, IMO :)
Title: Re: SpecBAS updated to v0.1323 with a new FOR EACH method
Post by: Tomaaz on Jul 24, 2023, 09:59 PM
Generic FOR[EACH] loop is a must. After trying it, there is no way back. :)

BTW Version v0.1323??? That looks like SpecBAS was created a couple of months ago. You need to change your versioning model. ;)
Title: Re: SpecBAS updated to v0.1323 with a new FOR EACH method
Post by: ZXDunny on Jul 26, 2023, 12:11 AM
Quote from: Tomaaz on Jul 24, 2023, 09:59 PMBTW Version v0.1323??? That looks like SpecBAS was created a couple of months ago. You need to change your versioning model. ;)

We're at v0.1417 now so disaster averted :D
Title: Re: SpecBAS updated to v0.1323 with a new FOR EACH method
Post by: Tomaaz on Jul 26, 2023, 01:16 PM
Come on. Look at micro(A). Version 12 ready to download. ;D