News:

Welcome to RetroCoders Community

Main Menu

SpecBAS updated to v0.1323 with a new FOR EACH method

Started by ZXDunny, Jul 19, 2023, 04:50 PM

Previous topic - Next topic

ZXDunny

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:



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!

CharlieJV


ZXDunny

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 :)

Tomaaz

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. ;)

ZXDunny

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

Tomaaz

Come on. Look at micro(A). Version 12 ready to download. ;D