Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 20:39 22 Nov 2024 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 : Array, number of elements

Author Message
ville56
Regular Member

Joined: 08/06/2022
Location: Austria
Posts: 91
Posted: 12:58pm 17 Oct 2024
Copy link to clipboard 
Print this post

I'm DIMing a float array with

DIM float xy(10)

OPTION BASE is not defined and thus is 0. I can access xy(0) to xy(10) which gives 11 elements. The manual says for DIM arrays the the NUMBER of elements is specified which would give xy(0) to xy(9). Why can i access the element xy(10) ....??? From my POV i should get an index out of bounds error. Btw, base and number of elements checked with BOUND ... no problems to see there.

Where did my thoughts thake the wrong exit?

Environment is WebMite, MMBasic 6.00.00RC8

Regards Gerald
                                                                 
73 de OE1HGA, Gerald
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6764
Posted: 01:36pm 17 Oct 2024
Copy link to clipboard 
Print this post

The array bounds are only checked at runtime.
The DIM statement index is for the last element, not the number of elements including the zeroeth one. :)

If you use OPTION BASE 1 then you get an error if you attempt to reference x(0), but not if you use OPTION BASE 0.

AFAIK this is pretty normal behaviour in BASIC.
.
Edited 2024-10-17 23:38 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
ville56
Regular Member

Joined: 08/06/2022
Location: Austria
Posts: 91
Posted: 02:24pm 17 Oct 2024
Copy link to clipboard 
Print this post

So this is an error in the manual as it says this is the number of elements not the index of the elements.
                                                                 
73 de OE1HGA, Gerald
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3194
Posted: 02:38pm 17 Oct 2024
Copy link to clipboard 
Print this post

Small point but OK, I will fix it.

Geoff
Geoff Graham - http://geoffg.net
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6764
Posted: 02:57pm 17 Oct 2024
Copy link to clipboard 
Print this post

I don't think I've ever used OPTION BASE 1. I just check the boundaries if necessary. :) After all, if it goes out of bounds you're going to have to trap the error anyway or you'll leave the user with a crashed program, so why bother?
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4219
Posted: 03:27pm 17 Oct 2024
Copy link to clipboard 
Print this post

Mick,

Sometimes option base 1 is handy.

In the thermal camera there is a lot of vendor provided arrays an indexes in the documentation. These where in base 1 (in example the left most pixel in the array is (1,1) and not (0,0). Rather than continuously changing the pointer when transferring data from vendor array to arrays that can be processed with MATH statements (and getting confused), I used option base 1.

Also option base 1 in (this is standard in basic) strings. There is no left$(a$,0). The first character has index 1.

Volhout
PicomiteVGA PETSCII ROBOTS
 
javavi

Senior Member

Joined: 01/10/2023
Location: Ukraine
Posts: 197
Posted: 03:42pm 17 Oct 2024
Copy link to clipboard 
Print this post

In the LUA programming language, all array indices start with 1 only.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6764
Posted: 03:54pm 17 Oct 2024
Copy link to clipboard 
Print this post

I knew there was something putting me off LUA.....   :)

If I want to use an array from 1 then I just ignore the zero element. I don't bother with OPTION BASE. I often DIM arrays slightly too big anyway. Sloppy, I know, but I'm rarely that stuck for RAM. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
ville56
Regular Member

Joined: 08/06/2022
Location: Austria
Posts: 91
Posted: 05:52pm 17 Oct 2024
Copy link to clipboard 
Print this post

Thank you Geoff and sorry guys, i didn't want to start a discussion about base 0 or 1 ... ;-).
I'm just hunting a bug and I wanted to make sure I understand at least the language elements i'm using.
                                                                 
73 de OE1HGA, Gerald
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 339
Posted: 12:07pm 18 Oct 2024
Copy link to clipboard 
Print this post

  Mixtel90 said  The array bounds are only checked at runtime.
The DIM statement index is for the last element, not the number of elements including the zeroeth one. :)

If you use OPTION BASE 1 then you get an error if you attempt to reference x(0), but not if you use OPTION BASE 0.

AFAIK this is pretty normal behaviour in BASIC.
.


I believe he's saying that, when using OPTION BASE 0, you can still access array(10) which should flag an error. OPTION BASE 1 does work properly.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9087
Posted: 01:04pm 18 Oct 2024
Copy link to clipboard 
Print this post

  Quote   I believe he's saying that, when using OPTION BASE 0, you can still access array(10) which should flag an error.


No it shouldn't

  Quote  The DIM statement index is for the last element, not the number of elements including the zeroeth one.


This correct

OPTION BASE 0 (default) creates an array from 0 to n where n is the dimension requested e.g. dim a(10) has elements 0-10. This is completely standard with most Basics and has been the way MMBasic works since its inception
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2109
Posted: 04:59pm 18 Oct 2024
Copy link to clipboard 
Print this post

dim sprite1(wh,bl,bl,bl,bl,bk,bk,bk,bk,bk,bk,bl,bl,bl,bl,wh,
bk,bl,re,re,re,bl,bl,bk,bk,bl,bl,re,re,re,bl,bk,
bk,bk,bk,bl,re,wh,bl,bk,bk,bl,wh,re,bl,bk,bk,bk,
bk,bk,bk,bl,re,wh,bl,bk,bk,bl,wh,re,bl,bk,bk,bk,
bk,bk,bk,bk,bl,wh,bl,bk,bk,bl,wh,bl,bk,bk,bk,bk,
bk,bk,bk,bk,bk,bl,bl,bk,bk,bl,bl,bk,bk,bk,bk,bk,
bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,
bk,bk,bk,bk,ye,ye,ye,bk,bk,ye,ye,ye,bk,bk,bk,bk,
bk,bk,bk,ye,bk,bk,bk,bk,bk,bk,bk,bk,ye,bk,bk,bk,
bk,ye,ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,ye,ye,bk,
bk,ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,ye,bk,
ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bl,ye,
ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,ye,
ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,ye,
bk,ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,ye,bk,
bk,bk,ye,bk,bk,bk,bk,bk,bk,bk,bk,bk,bk,ye,bk,bk)

but get Error : Expected closing bracket
using mmedit ??
posted on another thread. regards, stan
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9087
Posted: 05:02pm 18 Oct 2024
Copy link to clipboard 
Print this post

Of course you do. MMbasic has never supported multi-line statements. You were shown how to do it with DATA statements
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024