|
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: AustraliaPosts: 59 |
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: 10635 |
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: 5483 |
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: 59 |
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: 4150 |
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: 290 |
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: 59 |
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: 4150 |
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: 2834 |
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. |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |