Joined: 16/09/2019 Location: United KingdomPosts: 4048
Posted: 12:10pm 04 Mar 2022
Hi @al18,
MMBasic has a RESTORE command which is more powerful than that in most street BASICs and can do what I believe you are talking about.
The problem being wrangled with is that like most (all?) BASICs there is a single global "pointer" to DATA that the READ command reads from and there is no mechanism for the programmer to read that pointer and then having read it use that value to return the pointer to that state. Specifically the use of a single global pointer has limitations when for example you are partially through reading some DATA and then want to call a subroutine that needs to read some different DATA (or the same DATA from a different point).
This is crazy code, but does illustrate the point that whenever exiting mad() we want the data pointer to be the same as it was when we entered mad():
Option Base 0 Option Default None Option Explicit On
Restore my_data mad(5) End
Sub mad(depth%) If depth% = 0 Then Exit Sub Local data_pos% = Peek(DataPos) ' Cache the data pointer. Local s$ Restore my_data my_label: ' There seems to be an issue with using recursion and DO/FOR loops ? ' Might be MMB4L specific. Read s$ If s$ = "" Then Print Poke DataPos data_pos% ' Restore the data pointer. Exit Sub EndIf Print Space$(5 - depth%) Str$(depth%) s$ " "; If depth% > 1 Then Print mad(depth% - 1) Goto my_label End Sub
my_data: Data "A", "B", "C", "D", "E", ""
Note that PEEK(DATAPOS) and POKE DATAPOS don't currently exist, they are my proposed solution to the problem.