Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:59 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: Frustration with arrays....

     Page 1 of 3    
Author Message
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 02:00pm 11 Sep 2020
Copy link to clipboard 
Print this post

Ok, I'm once again going to voice my frustration with the way arrays are created in this version of BASIC....

I'm trying to come up with a way to make reusable library code that will work regardless of which way the main (calling) program sets OPTION MODE.

Yes I can use the BOUND command to understand the upper and lower array indexes of the array - that's not the problem.

The problem is in defining the arrays themselves...

In OPTION MODE 0  if I want an array to hold 10 elements I have to do:

dim integer myarray(9) = (1,2,3,4,5,6,7,8,9,10)

(WHY THE HELL IS IT 9?!?!? I'm putting 10 items into the array!!!! MAKES NO SENSE AT ALL)

In OPTION MODE 1 if I want the array to hold 10 elements I have to do:

dim integer myarray(10)= (1,2,3,4,5,6,7,8,9,10)

Shouldn't the dim integer myarray() take the SIZE, not the MAX INDEX count????

Also, since there's no way to know which way the OPTION MODE is set before I create the array, I have no way to know which version I should use...

Can we either:

1) standardize the value that goes in the array dimension to be the SIZE of the array (# of elements, regardless of the indexing scheme - so for my example above they BOTH would get set to 10)

or

2) get an MM.INFO(MODE) that will return either a 0 or 1 so we can ask which way the MODE is set???


Was the whole dimension taking the last index a hold over from MS's later versions of BASIC that switched to 0 indexing? Because I've never seen a single version of BASIC that did it based on last index rather than the size of the array...
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 02:03pm 11 Sep 2020
Copy link to clipboard 
Print this post

The handling of arrays is exactly the same as all other versions of MMBasic. It ain't going to change.
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 02:38pm 11 Sep 2020
Copy link to clipboard 
Print this post

  matherp said  The handling of arrays is exactly the same as all other versions of MMBasic. It ain't going to change.


Still seems weird to me...

If that's the case, can we at least get an MM.INFO(MODE) function that would give us what the current mode is set to so we can at least know what way the mode is set so we can create the array properly based on the mode?

Otherwise, reusable / portable code involving arrays is pretty much dead on arrival...
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 02:46pm 11 Sep 2020
Copy link to clipboard 
Print this post

Umm, not in front of my CMM2, but ...

Function array_mode()
 Local a(2)
 array_mode = Bound(a, 0)
End Function

MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 02:51pm 11 Sep 2020
Copy link to clipboard 
Print this post

... though I would agree that it would be comforting if all OPTIONs were queryable in a uniform manner.
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 02:53pm 11 Sep 2020
Copy link to clipboard 
Print this post

  thwill said  Umm, not in front of my CMM2, but ...

Function array_mode()
 Local a(2)
 array_mode = Bound(a, 0)
End Function


Edit: just looked at what you did more closely - yes I could do something like that but that's an ugly hack workaround..

If I do OPTION MODE 0

And then try to create my array with values in it (let's say I want to store 10)

dim myarray(9) = (1,2,3,4,5,6,7,8,9,10)

If OPTION MODE 1 is set and I use the same line of code I'll get a dimension size error (because it's only allocating 9 cells in the array but trying to put 10 in).

If I do it the other way around:

dim myarray(10) = (1,2,3,4,5,6,7,8,9,10)

This will work fine for OPTION MODE 1, but in OPTION MODE 0 I'll likewise get a dimension size error because it's expecting 11 items to be specified. (which seems dumb to me, but whatever, fine it's not going to get changed...)

There's currently no way to know which way OPTION MODE is set to, so no way for my library code to know which value to set the array size to to make it work in both cases... I have no way to know which way the calling program has it defined.


At least if there was an MM.INFO(MODE) type function then I could at least do something like:

dim size%
if (MM.INFO(MODE)=0) then
  size% = 9
else
  size% = 10
endif

dim myarray(size%) = (1,2,3,4,5,6,7,8,9,10)

Which would work in both OPTION MODE settings...
Edited 2020-09-12 00:58 by mkopack73
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 03:00pm 11 Sep 2020
Copy link to clipboard 
Print this post

Thwill's function does exactly what want. Just include a call at the top of your code and then you know how the option base is set
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 03:18pm 11 Sep 2020
Copy link to clipboard 
Print this post

You may also be able to do this:
Dim myarray(num_elements%(10)) = (1,2,3,4,5,6,7,8,9,10)

Function num_elements%(i%)
  num_elements% = i%
  If array_mode%() = 0 Then num_elements% = num_elements% - 1
End Function

Function array_mode%()
  Local a%(2)
  array_mode% = Bound(a%, 0)
End Function


Though I would probably cache the result of calling array_mode%() in a global variable.

Happy hacking,

Tom
Edited 2020-09-12 01:22 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 03:32pm 11 Sep 2020
Copy link to clipboard 
Print this post

Tom, any objections to me putting that code in my "reusable code" guide ?
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1427
Posted: 03:34pm 11 Sep 2020
Copy link to clipboard 
Print this post

  thwill said  ... though I would agree that it would be comforting if all OPTIONs were queryable in a uniform manner.



Micromites and Maximites! - Beginning Maximite
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 03:38pm 11 Sep 2020
Copy link to clipboard 
Print this post

BTW, I also did a little test to see if it was significantly slower to ask the array for it's bounds (like during a for loop) vs getting the bounds once and storing them in variables and then using the variables for setting the for loop index bounds...

It turns out they're so close in time (I did this over an array of size 100,000) that I wouldn't even bother worrying about it. Just put the bounds requests right in the for loop lower + upper index bounds...

I did this test because I was concerned that it would perform the bound() call on every iteration through the loop to see if the current index value was > than the upper bound (like it would if we were in C++ or Java) but looks like in MMBASIC it only grabs the values for the lower and upper for loop values once when declaring the loop.
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 03:40pm 11 Sep 2020
Copy link to clipboard 
Print this post

  mkopack73 said  Tom, any objections to me putting that code in my "reusable code" guide ?


No objections from me.

Best wishes,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:30pm 11 Sep 2020
Copy link to clipboard 
Print this post

I prefer to not assume either BASE 0 or BASE 1.
By filling the array after declaring, you can usually just ignore element zero.
Sometimes the old fashioned way is best.
If I am trying to code for both eventualities, I would prefer 'element 7' to be the same for both.

There are some functions where the BASE is significant, but not many and they may require the BASE to be fixed.

 OPTION BASE 0
 '
 mysub
 
 FOR n = 1 TO 10
   PRINT testarray(n)
 NEXT n
 
END
 
SUB mysub
 DIM testarray(10)
 RESTORE mysubdata
 FOR n = 1 TO 10
   READ testarray(n)
 NEXT n
mysubdata:
 DATA 1,2,3,4,5,6,7,8,9,10
END SUB


works the same with OPTION BASE 1

Jim
VK7JH
MMedit
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 10:28pm 11 Sep 2020
Copy link to clipboard 
Print this post

  TassyJim said  I prefer to not assume either BASE 0 or BASE 1.
By filling the array after declaring, you can usually just ignore element zero.
Sometimes the old fashioned way is best.
If I am trying to code for both eventualities, I would prefer 'element 7' to be the same for both.

There are some functions where the BASE is significant, but not many and they may require the BASE to be fixed.

 OPTION BASE 0
 '
 mysub
 
 FOR n = 1 TO 10
   PRINT testarray(n)
 NEXT n
 
END
 
SUB mysub
 DIM testarray(10)
 RESTORE mysubdata
 FOR n = 1 TO 10
   READ testarray(n)
 NEXT n
mysubdata:
 DATA 1,2,3,4,5,6,7,8,9,10
END SUB


works the same with OPTION BASE 1

Jim


Sure, but now you're both 1) wasting a cell 2) leaving the potential for unintended consequences because you might not use it but somebody else using your code might use it not realizing that you intended it to stay empty...
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 05:10pm 12 Sep 2020
Copy link to clipboard 
Print this post

  mkopack73 said  
In OPTION MODE 0  if I want an array to hold 10 elements I have to do:
dim integer myarray(9) = (1,2,3,4,5,6,7,8,9,10)
(WHY THE HELL IS IT 9?!?!? I'm putting 10 items into the array!!!! MAKES NO SENSE AT ALL)


No you don't.

if you want elements numbered 1 to 10, DIMimg x(10) just do it. It will work in any OPTION BASE. Just ignore (0). You are over thinking this. Do not fall into the trap of "wasting". you have half a meg to play with. If you are worried about a few bytes unused, you had batter stop using . in your comments, spaces before or after ' etc.

p.s. this is the same in every basic I have ever used
Edited 2020-09-13 03:13 by CaptainBoing
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 05:25pm 12 Sep 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  No you don't.

if you want elements numbered 1 to 10, DIMimg x(10) just do it. It will work in any OPTION BASE. Just ignore (0). You are over thinking this. Do not fall into the trap of "wasting". you have half a meg to play with. If you are worried about a few bytes unused, you had batter stop using . in your comments, spaces before or after ' etc.


But whatever you do, do not SORT the array or that zero value hiding in element 0 will bite you in the arse.

Kind regards,

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 05:42pm 12 Sep 2020
Copy link to clipboard 
Print this post

  CaptainBoing said  If you are worried about a few bytes unused, you had better stop using . in your comments, spaces before or after ' etc.


True of most MMBasics, but I do not believe it applies to the CMM2 which tokenises the BASIC before loading it onto the flash (or equivalent if using OPTION RAM).

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 08:00pm 12 Sep 2020
Copy link to clipboard 
Print this post

Coming to realize that there are 2 very different groups here... Those who do professional development and those who don't....

Edit: Or rather I should say, those who have formal software engineering computer science educations and those who don't...
Edited 2020-09-13 06:04 by mkopack73
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 08:09pm 12 Sep 2020
Copy link to clipboard 
Print this post

  mkopack73 said  Coming to realize that there are 2 very different groups here... Those who do professional development and those who don't....


Those are fighting words partner

I'm sure there are several different groups here, but the primary split is probably between the long term residents with a background in electronics, microcontroller programming and firmware vs. the upstarts with a background in application programming.

I'm sure we can all get along and learn from each other.

Also worth remembering that it is the first group that generates the toys that the second group can then use to show-off.

YMMV,

Tom
Edited 2020-09-13 06:09 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 08:55pm 12 Sep 2020
Copy link to clipboard 
Print this post

  mkopack73 said  ... Or rather I should say, those who have formal software engineering computer science educations and those who don't...


  thwill said  Those are fighting words partner


Indeed. Or those who have no computer science education but a software applications developer / systems analyst / tech writer background starting in 1970 and hardware tinkering since 1977.

Writing code in C since before a good many on here were born, but for hobby electronics, preferring BASIC.

There are many quite experienced hardware and software people on this site, including some who started when formal training was pretty skimpy. Besides, as Dogberry says, "Comparisons are odorous".

If working in BASIC, "formal software engineering computer science education" purists are pretty much self-excluding.

~
Edited 2020-09-13 06:58 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
     Page 1 of 3    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025