Question about prgedit
Root / Programming Questions / [.]
raimondzCreated:
Interesting question. If I had to guess, completely blindly and baselessly, I'd say there's a high chance this command would work:
PRGEDIT 2, @LABEL
If true, this would be a classic example of an undocumented feature, there's no shortage of those in SB. And if it does work, the question arises: can you get the line number you're currently at in PRGEDIT? A quick glance at the reference doesn't seem like it.
tl;dr dunno, sorry for wasting your time
Put a FOR loop iterating through your program that constantly runs PRGGET$() on each line. If PRGGET$() equals the line you're looking for (plus CHR$(10)) then the indexer in the FOR loop will tell you the line number it was on.
A WHILE loop would be better than a FOR loop.
Interesting question. If I had to guess, completely blindly and baselessly, I'd say there's a high chance this command would work: PRGEDIT 2, @LABEL If true, this would be a classic example of an undocumented feature, there's no shortage of those in SB. And if it does work, the question arises: can you get the line number you're currently at in PRGEDIT? A quick glance at the reference doesn't seem like it. tl;dr dunno, sorry for wasting your timeI tried to do that but it didn't work. However, i saw that the function let you jump to a line x, so I guess I'm going use that. The reason why i don't want to use a for or a while is because I want to jump at a certain line in O(1), not in O(n). I though that I could jump to labels because smilebasic store the reference to their lines(for goto, gosub, restore and copy)
I tried to do that but it didn't work. However, i saw that the function let you jump to a line x, so I guess I'm going use that.Ah well, it was worth a shot :P
The reason why i don't want to use a for or a while is because I want to jump at a certain line in O(1), not in O(n). I though that I could jump to labels because smilebasic store the reference to their lines(for goto, gosub, restore and copy)In that case, you could do the same thing by yourself: go through the whole file once in your init routine, find the labels you want (or all of them if you can think of a way), store them somewhere, and after that whenever you want to go to them you just use the values you saved.
Hmm, I wonder if there is a way to throw a silent, innocuous error at the label (intentionally), and then just check the ERRLINE system var. I find it odd that SmileBASIC doesn't have an equivalent to the C preprocessor directive __LINE__. Would be incredibly useful.
Thete's nothing that sets ERRLINE that doesn't also terminate the program. As for __LINE__, you could request it on the Miiverse feature request thread.
Actually, PRGEDIT seems to be itself O(n), so iterating through all lines is sadly O(n^2). Generally, it's better to LOAD a program file into a string variable and parse through the lines manually using INSTR or the like, if possible.
Edit: Now I discovered that if you call PRGEDIT once to get the first line (which I believe is constant time), then PRGGET$() will read each line in succession, at an extremely reasonable speed. This is another option, I suppose.
As for __LINE__, you could request it on the Miiverse feature request thread.Thanks, SquareFingers, I've added that to the developer requests on the miiverse page.