|
Forum Index : Microcontroller and PC projects : MMbasic n selected sub load global array, odd issue data not stored?
| Page 1 of 2 |
|||||
| Author | Message | ||||
| Supertech Regular Member Joined: 13/11/2016 Location: AustraliaPosts: 60 |
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. Anybody know why? Regards, supertech. |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10646 |
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: NetherlandsPosts: 5485 |
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: AustraliaPosts: 182 |
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: AustraliaPosts: 60 |
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 KingdomPosts: 4154 |
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: AustraliaPosts: 182 |
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: AustriaPosts: 292 |
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: AustraliaPosts: 60 |
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: AustraliaPosts: 182 |
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 KingdomPosts: 4154 |
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: AustraliaPosts: 2837 |
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. Edit If the array is less than 64 cells perhaps you don't need an array at all. FLAG can hold true/false values in an integer. Edited 2025-11-22 07:20 by phil99 |
||||
| zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 626 |
( In other systems far far away at least ... )the logic clause needs brackets... otherwise eg in "if n = 8 ..." n is set to 0 or 8 etc, the setting operation was successful and returns TRUE so try "if (n = 8)"... ed = also i'd avoid using "true" , too similar to system Edited 2025-11-22 07:27 by zeitfest |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2837 |
Removing the elses. Option EXPLICIT Option DEFAULT NONE Const true = 1234 'make one correct Const true2 = 3465 'make another one correct Const true3 = 9999 'and another 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 : Print n, Condn(3)' Else Condn(n) = 0 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 : Print n, Condn(13)' Else Condn(n) = 0 If (n = 14) And (true2 = 3465) Then Condn(n) = 1 : Print n, Condn(14)' Else Condn(n) = 0 If (n = 15) And (true3 = 9999) Then Condn(n) = 1 : Print n, Condn(15)' Else Condn(n) = 0 Next n Print "Condn(3) ="Condn(3)", but should be a 1 stored in Condn(3) as Above, but is 0 ?" Print "Condn(14) ="Condn(14)", but should be a 1 stored in Condn(14) as Above, but is 0 ?" Print "Condn(15) ="Condn(15)", but should be a 1 stored in Condn(15) as Above, but is 0 ?" Math V_Print Condn() End Sub Math V_Print Condn() > RUN Simulate array storage issue 3 1 13 1 14 1 15 1 Condn(3) = 1, but should be a 1 stored in Condn(3) as Above, but is 0 ? Condn(14) = 1, but should be a 1 stored in Condn(14) as Above, but is 0 ? Condn(15) = 1, but should be a 1 stored in Condn(15) as Above, but is 0 ? 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 > Using FLAG Option EXPLICIT Option DEFAULT NONE Const true = 1234 'make one correct Const true2 = 3465 'make another one correct Const true3 = 9999 'and another one correct 'Dim integer Flags =0 '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 Flag(n) = 1' Else Condn(n) = 0 If (n = 1) And (true = 1111) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 2) And (true = 4321) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 3) And (true = 1234) Then Flag(n) = 1 : Print n, Flag(3)' Else Condn(n) = 0 If (n = 4) And (true = 5684) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 5) And (true = 1540) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 6) And (true = 1346) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 7) And (true = 4852) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 8) And (true = 6784) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 9) And (true = 3468) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 10) And (true = 2675) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 11) And (true = 3457) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 12) And (true = 1986) Then Flag(n) = 1' Else Condn(n) = 0 If (n = 13) And (true = 1234) Then Flag(n) = 1 : Print n, Flag(13)' Else Condn(n) = 0 If (n = 14) And (true2 = 3465) Then Flag(n) = 1 : Print n, Flag(14)' Else Condn(n) = 0 If (n = 15) And (true3 = 9999) Then Flag(n) = 1 : Print n, Flag(15)' Else Condn(n) = 0 Next n Print "Condn(3) ="Flag(3)", but should be a 1 stored in Condn(3) as Above, but is 0 ?" Print "Condn(13) ="Flag(13)", but should be a 1 stored in Condn(3) as Above, but is 0 ?" Print "Condn(14) ="Flag(14)", but should be a 1 stored in Condn(14) as Above, but is 0 ?" Print "Condn(15) ="Flag(15)", but should be a 1 stored in Condn(15) as Above, but is 0 ?" Print :Print Bin$(MM.FLAGS,16) End Sub Print :Print MM.FLAGS Simulate array storage issue 3 1 13 1 14 1 15 1 Condn(3) = 1, but should be a 1 stored in Condn(3) as Above, but is 0 ? Condn(13) = 1, but should be a 1 stored in Condn(3) as Above, but is 0 ? Condn(14) = 1, but should be a 1 stored in Condn(14) as Above, but is 0 ? Condn(15) = 1, but should be a 1 stored in Condn(15) as Above, but is 0 ? 1110000000001000 57352 > Edited 2025-11-22 09:49 by phil99 |
||||
| Supertech Regular Member Joined: 13/11/2016 Location: AustraliaPosts: 60 |
BTW, my last comment on this subject. The purpose of this array Condn() scan is, 1. Scan live items of change dynamically and add if fail or subtract, each and every one should they occur. Eg Overheating or no longer Overheating. This as Only one of 16, that could be active at any time, all stored ready. 2. With above then referred to CondMsg$() corresponding to appropriate message to suit. In example "Overheating" stored here. Quite simple. So Condn() collects 1 or 0, while CondMsg$() displays textual issue if exists. This is also important to be logged efficiently later. I just wanted to further advance this which revealed the odd issue encountered Thank you to those that understand objectively. Appreciated. Regards. Supertech. |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2837 |
For such repeated operation start the Sub with MATH SET 0, Condn() to clear the array then fill with the new values. If using the FLAG method start the Sub with FLAGS = 0. |
||||
| zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 626 |
So is there resolution ? what is it ? |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10646 |
The resolution is that the original MMbasic code was incorrect. Every line does a test and if it passes sets condn(n) to 1. All OK so far. However, the next line fails the test because n doesn't meet its condition. Therefore it immediately sets condn(n) back to zero. Remember, every test is actioned each time through the loop for every value of n Think about the first two lines with n=0 and true=9568 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 The first line passes the test and sets condn(0) to 1 Then the second line is run. n is still 0 so it fails the test and actions the else clause setting condn(0) back to 0 It is just incorrect code. If you replace the ifs with a select case on n then you will only action the relevant test for that value of n select case n case 0 if true = 9568 Then Condn(n) = 1' Else Condn(n) = 0 case 1 if true = 1111 Then Condn(n) = 1' Else Condn(n) = 0 etc. end select Edited 2025-11-22 18:48 by matherp |
||||
| Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 8315 |
You can also use SELECT CASE like this SELECT value CASE value <1000 and value >3000 array(1) = 1 CASE value >3000 array(9) = 1 CASE ELSE anything you like END SELECT Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2837 |
For minimum change to the original code I commented out the elses but perhaps should have deleted them to make it more obvious. Then to make original code work set the array to 0 at the start of the Sub. 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 MATH SET 0, Condn() 'Scan, tag condition or cancel dynamic array for n = 0 to 15 if n = 0 and true = 9568 then Condn(n) = 1 if n = 1 and true = 1111 then Condn(n) = 1 if n = 2 and true = 4321 then Condn(n) = 1 if n = 3 and true = 1234 then Condn(n) = 1 : print n, Condn(3) if n = 4 and true = 5684 then Condn(n) = 1 if n = 5 and true = 1540 then Condn(n) = 1 if n = 6 and true = 1346 then Condn(n) = 1 if n = 7 and true = 4852 then Condn(n) = 1 if n = 8 and true = 6784 then Condn(n) = 1 if n = 9 and true = 3468 then Condn(n) = 1 if n = 10 and true = 2675 then Condn(n) = 1 if n = 11 and true = 3457 then Condn(n) = 1 if n = 12 and true = 1986 then Condn(n) = 1 if n = 13 and true = 1234 then Condn(n) = 1 if n = 14 and true = 3465 then Condn(n) = 1 if n = 15 and true = 9999 then Condn(n) = 1 next n print "Condn(3) ="Condn(3)", but should be a 1 stored in Condn(3) as Above, but is 0 ?" end sub |
||||
| Page 1 of 2 |
|||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |