Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:14 22 Nov 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 : MMbasic n selected sub load global array, odd issue data not stored?

Author Message
Supertech

Regular Member

Joined: 13/11/2016
Location: Australia
Posts: 59
Posted: 02:41pm 21 Nov 2025
Copy link to clipboard 
Print this post

This is the issue. Ive tried this in Geoff MMBasic 5.05.05 and in RP2040 FW 6.00.03 which both does same. Array data in my Condn(n) IS collected and set, however lost as soon as outside "for n = 0 to 15".

Is this an illegal use of a sub or MMBasic limitation?, or something else?
Only happens if "for n = 0 to 15" is used. haven't tried outside a sub, but if in a sub without "for n = 0 to 15", in direct numbered statements, its fine.

  Quote  
OPTION EXPLICIT
OPTION DEFAULT NONE

Const true = 1234 'just make one correct
dim integer Condn(15) 'store 15 (from zero) 'Conditions detected'

print "Simulate array storage issue" : print

ScanCondsim

'Updated 08/11/25.
sub ScanCondsim 'Collect Scan, for latter Display .... Conditions as Status,
local integer n
'Scan, tag condition or cancel dynamic array
for n = 0 to 15
 if n =  0 and true = 9568 then Condn(n) = 1 else Condn(n) = 0
 if n =  1 and true = 1111 then Condn(n) = 1 else Condn(n) = 0
 if n =  2 and true = 4321 then Condn(n) = 1 else Condn(n) = 0
 if n =  3 and true = 1234 then Condn(n) = 1 else Condn(n) = 0 : print n, Condn(3)
 if n =  4 and true = 5684 then Condn(n) = 1 else Condn(n) = 0
 if n =  5 and true = 1540 then Condn(n) = 1 else Condn(n) = 0
 if n =  6 and true = 1346 then Condn(n) = 1 else Condn(n) = 0
 if n =  7 and true = 4852 then Condn(n) = 1 else Condn(n) = 0
 if n =  8 and true = 6784 then Condn(n) = 1 else Condn(n) = 0
 if n =  9 and true = 3468 then Condn(n) = 1 else Condn(n) = 0
 if n = 10 and true = 2675 then Condn(n) = 1 else Condn(n) = 0
 if n = 11 and true = 3457 then Condn(n) = 1 else Condn(n) = 0
 if n = 12 and true = 1986 then Condn(n) = 1 else Condn(n) = 0
 if n = 13 and true = 1234 then Condn(n) = 1 else Condn(n) = 0
 if n = 14 and true = 3465 then Condn(n) = 1 else Condn(n) = 0
 if n = 15 and true = 9999 then Condn(n) = 1 else Condn(n) = 0
next n
print
print "Condn(3) ="Condn(3)", but should be a 1 stored in Condn(3) as Above, but is 0 ?"
end sub


Anybody know why?
Regards, supertech.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10635
Posted: 03:01pm 21 Nov 2025
Copy link to clipboard 
Print this post

print "Condn(3) ="Condn(3)", but should be a 1 stored in Condn(3) as Above, but is 0 ?"

That can't be the correct statement, what is the real program?
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5483
Posted: 03:38pm 21 Nov 2025
Copy link to clipboard 
Print this post

Super.

print "Condn(3) =";Condn(3);", but should be a 1 stored in Condn(3) as Above, but is 0 ?"


You need semi colums to separate items in a print statement.

Volhout
PicomiteVGA PETSCII ROBOTS
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 182
Posted: 03:45pm 21 Nov 2025
Copy link to clipboard 
Print this post

G'day Supertech,
I spent ages with something similar only to realize that on every iteration of the loop the ELSE statement resets the array elements.

If you set TRUE to 9999 then Condn(15) will = 1 as its the last loop.

Regards,
Lyle.
 
Supertech

Regular Member

Joined: 13/11/2016
Location: Australia
Posts: 59
Posted: 03:52pm 21 Nov 2025
Copy link to clipboard 
Print this post

  matherp said  
print "Condn(3) ="Condn(3)", but should be a 1 stored in Condn(3) as Above, but is 0 ?"

That can't be the correct statement, what is the real program?


the real bits in my project, substituted as posted renamed to test constant just to illustrate issue of assigned array lost.

OK, A Sample of real, (all 15 work fine, when just number assigned),
here tis,
if (TPbadn > 5 or TP! = 1000) then Condn(3) = 1 else Condn(3) = 0
This one of 15 works fine,

however, I upgraded my sub in this real sample ( 3rd of 15) to,
for n = 0 to 15
...etc
...etc
if n = 3 AND (TPbadn > 5 or TL! = 1000) then Condn(n) = 1 else Condn(n) = 0  
...etc
...etc
next n

Should be fine yes?

In fact it dosent seem to matter what they are here * past
"if n = ? and ? then Condn(n) = 1 else Condn(n) = 0", which stores as per print! BUT; ONLY while inside n = 0 to 15 loop in the sub. Once loop n finished, all array of so called stored Condn(n) is lost, or zeroed even before leaving sub!?

I just substituted my data for the 'const true' for showing the issue at hand. I was very supersized when my collected faults vanished due to adding to my real sub the convenience of 'For n = 0 to 15' combined to Condn(n) assignments 1 else 0. Simples.

It just dumps array data or perhaps never stored it?
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4150
Posted: 04:02pm 21 Nov 2025
Copy link to clipboard 
Print this post

  Supertech said  Anybody know why?
Regards, supertech.

Most of the lines set Condn(n) to 0, not just once but many times.

So, when the line
if n=3 and ...
sets Condn(n) to 1

the next several lines each set it to 0,

I don't know what code you meant but the tests are wrong for whatever you wanted.

To put it another way, each of your elses happens nearly all the time regardless of n.

Summary: MMBasic is working but your code isn't.

BTW your PRINTs ought to have semicolons (or commas) but MMBasic is "lax" in not requiring them.

John
Edited 2025-11-22 02:07 by JohnS
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 182
Posted: 04:20pm 21 Nov 2025
Copy link to clipboard 
Print this post

G'day Supertech,
Please see my post above.

Pretty sure the last loop with n=15 will set all preceding values of Condn(n) to zero due to the ELSE statement, as "IF N=0.....ELSE Condn(0) = 0" with n=15 is going to trigger the else statement

Also reckon the FOR / NEXT loop is redundant as it will set all the values in a single pass, or is this just for demonstrating the fault?

This assumes I understand the logic correctly  

Regards,
Lyle.
 
ville56
Senior Member

Joined: 08/06/2022
Location: Austria
Posts: 290
Posted: 04:27pm 21 Nov 2025
Copy link to clipboard 
Print this post

  Quote  
print "Condn(3) =";Condn(3);", but should be a 1 stored in Condn(3) as Above, but is 0 ?"


You need semi colums to separate items in a print statement.


the statement works correctly and does not throw an error, the interpreter is rather relaxed with it.  
                                                                 
73 de OE1HGA, Gerald
 
Supertech

Regular Member

Joined: 13/11/2016
Location: Australia
Posts: 59
Posted: 04:31pm 21 Nov 2025
Copy link to clipboard 
Print this post

  mozzie said  G'day Supertech,
I spent ages with something similar only to realize that on every iteration of the loop the ELSE statement resets the array elements.

If you set TRUE to 9999 then Condn(15) will = 1 as its the last loop.

Regards,
Lyle.


Mozzie, certainly makes sense to me. Thank you.  
The why and ways around are more important though. Cant help thinking the ELSE formating might be the answer or even ditch ELSE. I need dynamic results. Ill find another way.
 
mozzie
Senior Member

Joined: 15/06/2020
Location: Australia
Posts: 182
Posted: 05:03pm 21 Nov 2025
Copy link to clipboard 
Print this post

Supertech,

The following works on MMbasic for DOS, not sure if its the best solution:

If N = 3 then IF true = 1234 then Condn(n) = 1 else Condn(n) = 0


Pretty sure someone here will have a better way if you need to test the value of True depending on the value of N every loop. Select case or choose perhaps?

Hope this helps  

Regards,
Lyle.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4150
Posted: 05:09pm 21 Nov 2025
Copy link to clipboard 
Print this post

The reason it fails to do what is wanted is that the else part happens almost all the time for each line.

The code is roughly doing
if (...) else Condn(n)=0
where the (...) part is nearly always false.

Maybe say what you're trying to do as the whole for loop and those elses make no sense.

John
Edited 2025-11-22 03:11 by JohnS
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2834
Posted: 08:44pm 21 Nov 2025
Copy link to clipboard 
Print this post

  Quote   those elses make no sense
That is how I see it too.
Start with the array set to 0 (MATH SET first if the array gets used more than once) then use the required logic to set TRUE cells to 1, FALSE cells remain 0.
 
Print this page


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

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025