Minimising Loops With Program Constants

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Minimising Loops With Program Constants

Post by MattC »

Hi,

Again, trying to work out if my logic is the simplest or if there's an easier, more logical way that I'm missing.

I have a program which uses program constants (not Windows), which are used as references to array elements, but are not necessarily consecutive. However, several of them need to be accessed alongside each other. A simple example is this:

Code: Select all

      REM program constants
      OPT_A = 0
      OPT_B = 2
      OPT_C = 5
      REM others...

      DIM opt%(9), result%(2)

      REM test data
      FOR I% = 0 TO 9
        opt%(I%) = RND(2) = 1
      NEXT

      REM main routine
      pos% = -1
      IF opt%(OPT_A) THEN
        pos% += 1
        result%(pos%) = RND(10)
      ENDIF
      IF opt%(OPT_B) THEN
        pos% += 1
        result%(pos%) = RND(10)
      ENDIF
      IF opt%(OPT_C) THEN
        pos% += 1
        result%(pos%) = RND(10)
      ENDIF

      REM printout
      IF pos% >= 0 THEN
        FOR I% = 0 TO pos%
          PRINT result%(I%)
        NEXT
      ENDIF
This is basically what's happening (not the RND) in my coding, but there's an awful lot more going on. I would like to remove the duplication. The only method I can come up with is to use a 'constant array', which holds the list of constant values:

Code: Select all

      REM program constants
      OPT_A = 0
      OPT_B = 2
      OPT_C = 5
      REM others...

      DIM opt%(9), result%(2), optconst%(2)

      REM test data
      FOR I% = 0 TO 9
        opt%(I%) = RND(2) = 1
      NEXT

      REM main routine
      optconst%() = OPT_A, OPT_B, OPT_C
      pos% = -1
      FOR I% = 0 TO 2
        IF opt%(optconst%(I%)) THEN
          pos% += 1
          result%(pos%) = RND(10)
        ENDIF
      NEXT

      REM printout
      IF pos% >= 0 THEN
        FOR I% = 0 TO pos%
          PRINT result%(I%)
        NEXT
      ENDIF
This does have the same effect and is neater, but I was wondering if there was a better way to do it - perhaps without using the optconst%() array?

Matt
DDRM

Re: Minimising Loops With Program Constants

Post by DDRM »

Hi Matt,

Looks OK to me! ;-)

I guess you could assign the contents of the optconsts% array directly, rather than assigning the value to a variable first, which would save a little space and time.

Not sure why you don't like the array approach? The memory and speed overhead will be minimal, and the win in terms of code compactness and clarity seems worthwhile. If you are worried about space, and you never need an index greater than 256, you could use a byte array, but that hardly seems worth it. If you really wanted to be opaque you could code your variables as a string, using the character value.... but I wouldn't recommend it! ;-)

Best wishes,

D
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: Minimising Loops With Program Constants

Post by MattC »

Thanks D,
DDRM wrote: Sun 06 Feb 2022, 17:33 you could assign the contents of the optconsts% array directly, rather than assigning the value to a variable first
That's true, but the reason I put constants in is that, due to previous problems encountered, I may have to add further opt levels (elements) and mess around with the values (it's a little complicated, too much so to go into here), so having the constants would mean that, when that happened, I would not need to find out each and every time I used, say, 'opt%(23)' and change it to 'opt%(24)' - which may occur many times. I would just have to change the constant OPT_A = 23 to OPT_A = 24 and the whole thing would change accordingly. However, I agree, this does cause problems of its own. Hence the question.
DDRM wrote: Sun 06 Feb 2022, 17:33 Not sure why you don't like the array approach?
It's not so much that I don't like it, just that I had hoped there might be an easier way. But perhaps not. The more I look at it, the more reasonable it seems. I think the problem was that the subroutine that I needed it in already had a significant number of LOCAL variables, so I didn't really want to add another array. But, hey, what's one more. :)

Matt