curly
Full Member
Posts: 161
|
Post by curly on Mar 19, 2021 10:15:26 GMT -5
[SaveSettings] open "Settings.csv" for output as #2 for i = 1 to 11 for j = 1 to 6 print #2, user$(i,j) if user$(i,1) = "BLANK" then goto [EndSaveSettings] end if next j next i [EndSaveSettings] close #2 return
Good afternoon,
I have quite a bit program, about 2500 lines. Part of it includes a 2 dimensional array, settings$(11,6). Part of the program changes some of the values and calls the subroutine attached to save it. This part works fine.
When I close the program using a trapclose routine, it call this same subroutine to ensure it is up to date, but produces an empty file.
After an hour or so with the debugger, it appears that after 'confirm "Are you sure you want to quit?"; con$, and the subrouting is called, I spotted in the debugger window that i remains constant at 0 even though the debugger is stepping through the for next loop?
Any suggestions please?
|
|
|
Post by tsh73 on Mar 19, 2021 10:56:50 GMT -5
Could that be that known bug? libertybasiccom.proboards.com/thread/869/array-scope-indexIn my test, variable did not change (but it should be GLOBAL and FOR loop should be inside a SUB) Is the file empty (size 0) or it indeed contains 66 empty lines? Change printing to a file to ordinary PRINT (to mainwin) and see if you actually print anything.
|
|
|
Post by Rod on Mar 19, 2021 11:55:17 GMT -5
Also you should not be jumping out for next loops like that.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Mar 19, 2021 12:56:47 GMT -5
Hi Rod and tsh73,
Your guidance is really appreciated, but I don't think I'm aware of an alternative to jumping out when you've found what you want?
Please elaborate or suggest so I can get better.
Kind regards
|
|
|
Post by Chris Iverson on Mar 19, 2021 13:51:27 GMT -5
The EXIT FOR command should be used to terminate a loop early, and you'd have to use two of them to jump out of two loops.
Jumping out of a loop(or multiple loops) directly can cause internal issues in LB, where parts of it still think it's in the loops(because they never terminated).
|
|
|
Post by Rod on Mar 19, 2021 14:10:08 GMT -5
I dont see where you are inserting the " or , associated with csv files either. But you may be using line input to read it back in. In which case it is not a csv file. But thats only a name.
[SaveSettings] open "Settings.csv" for output as #2 for i = 1 to 11 for j = 1 to 6 print #2, user$(i,j);","; next j next i close #2 return
So you now have some blank values, but you did not jump out the for next loops and you inserted the necessary , for a csv file. If indeed we are creating a .csv "data","data", or data,data, is how a .csv file looks. If you read with line input then it is really just a sequential file and CRLF pairs separate each data item.
But we are way off track of the original fault, printing the output to see what is happening is the way to debug it. Showing how you read the file will help also.
|
|
|
Post by tenochtitlanuk on Mar 19, 2021 14:29:48 GMT -5
Actually I think you re confusind subs ( which are 'called' in LB) and gosubs, which end by returning.
Look at the example below- it creates a csv file whether called as a sub, or when called by trapclose.
Results are three files- and a screen display which closes. We're not exactly sure what the conditions are for your "BLANK" but this should give you an idea??
nomainwin
global p$ p$ ="Stay"
open "Demo" for text as #w ' so can see the generated terms..
#w "!trapclose QuitAndSave"
dim user$( 11, 6)
for i =1 to 11 for j =1 to 6 user$( i, j) =chr$( 65 +int( 26 *rnd( 1))) +chr$( 48 +int( 10 *rnd( 1))) #w user$( i, j) +","; next j #w "" next i
call QuitAndSave p$
p$ ="stillStay" call QuitAndSave p$
p$ ="Quitting"
wait
sub QuitAndSave p$ if p$ ="#w" then p$ ="Quitting" open "Settings_" +p$ +".csv" for output as #fOut for i = 1 to 11 for j = 1 to 6 #fOut user$( i, j) +","; next j #fOut chr$( 13); next i
close #fOut
if p$ ="Quitting" then close #w: end ' if we are really finishing... end sub
|
|
|
Post by Rod on Mar 19, 2021 14:35:06 GMT -5
I will be remaining constant because you are always jumping out and avoiding the i's next statement.
|
|
|
Post by Carl Gundel on Mar 19, 2021 20:24:54 GMT -5
The EXIT FOR command should be used to terminate a loop early, and you'd have to use two of them to jump out of two loops. Jumping out of a loop(or multiple loops) directly can cause internal issues in LB, where parts of it still think it's in the loops(because they never terminated). With Liberty BASIC 5 this is no longer an issue. The following code blows up in an unmanaged way in LB4, but in LB5 is gives a proper error message 'Branch label not found: [jumpIn]' because the code inside the loop is scoped and the branch label is only visible inside the loop. goto [jumpIn] for x = 1 to 10 [jumpIn] print x next x print "Done." The following code does not work properly in LB4. The outer loop gets confused. In LB5 it works as you would expect it to. for x = 1 to 100000 for y = 1 to 10 if rnd(1) > 0.9 then [skip] next y [skip] next x print "Done." Essentially with LB5 it manages the scope and structure of the program such that if a branch label cannot be safely jumped to you will get a proper runtime error. It will not quietly misbehave.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Mar 20, 2021 5:07:29 GMT -5
Thank you all - a lot to digest at once. I have about 15 'for next' loops that I jump out of when a target is found. Would you suggest changing them for while/wend, do/until etc?
With regards to CSV files, by using 'print #2, one variable at a time, the commas are inserted automatically, and I am reading them in in a similar way successfully. When getting the file in, I again us a for next loop to read the items of data in one at a time using 'input #1, system$(i)'.
The bug has disappeared since I changed 'for i' to 'for q' in that particular subroutine. Everything is now funtioning as I intended.
If it is felt I shoud only use 'for next' loops if they are run to completion, I will try changing what I have to those I have listed in the first line of this reply? (while wend/ do until etc)
'For next' was one of the first things I discovered back in the late 1970s on my Dragon 64 (I am 78), and it has somewhat stuck with me as the first port of call?
|
|
|
Post by Rod on Mar 20, 2021 6:07:03 GMT -5
Here is the code with exit for inserted.
dim user$(10,10) for n= 1 to 10 for m= 1 to 10 user$(n,m)="Hello" next next user$(5,5)="BLANK"
gosub [SaveSettings] wait
[SaveSettings] open "Settings.csv" for output as #2 for i = 1 to 11 for j = 1 to 6 #2 user$(i,j) if user$(i,j) = "BLANK" then exit for next j if user$(i,j)="BLANK" then exit for next i close #2 return
For Next loops are perfectly fine to use. The point is that ANY kind of loop needs a proper exit. If you jump from within loops you are storing up trouble. Its ok to jump if you know you will come back, gosub, function() Sub etc
The other thing you have wrong is the file handling. Not wrong so much as you are not actually doing what you think you are doing. #2, user$(x,y) for example does NOT insert a comma.
You are creating the file but as a sequential file with each data item on its own line terminated by CRLF. Out of time right now but will post more on file handling. Yes what you have works but you need to understand it a little better.
|
|
|
Post by tsh73 on Mar 20, 2021 8:15:26 GMT -5
This is implementation-specific In this very version - LB 4.5.1 - (also probably previous LB 4.x and all JB) it is so But As Carl said, in LB 5 it will not be a problem. And I read book on Pascal recently - jumping out of nested loops said to be proper way for Pascal.
|
|
|
Post by Rod on Mar 20, 2021 10:14:01 GMT -5
Perhaps Pascal terminates the loop correctly when you jump. As we know Liberty does not. Even in LB5 it may still be bad practice. I did not read Carls post as saying its "ok" I read it as saying it will be "managed", perhaps with a runtime error report.
Anyways so as not to confuse Curly too much. If you are leaving a loop, and don't intend to return at exactly the same point and continue, then you should exit the loop and stop it. Exit for, Exit while, Exit do are all available for this purpose.
Now the file. You called it a CSV file. It isnt. A CSV file has the data separated by a , and sometimes also " So if you look at csv file with Notepad you see
"data","data","data"
If you look at the file you create with notepad you will see data data data
So no commas and each data item is on its own line. Nothing wrong with that its just NOT a csv file.
When you code print #2, "data". Only "data" is sent to the file. the comma is ignore. "data" is followed by CRLF which is the end of line marker used in Windows. These two hidden characters are always added when you print to a file unless you add a trailing ; These end of line characters also let you pull the data back in a line at a time.
Why is the #2, comma ignored? Well it is an old way of coding. Old syntax is print #2, "data" The new way is #2 "data" Shorter and simpler, no print required and no comma required. The comma used to be necessary but Liberty ignores it now.
Enough confusion for now just keep on coding and it will all become clear.
|
|
|
Post by Carl Gundel on Mar 20, 2021 10:55:45 GMT -5
Perhaps Pascal terminates the loop correctly when you jump. As we know Liberty does not. Even in LB5 it may still be bad practice. I did not read Carls post as saying its "ok" I read it as saying it will be "managed", perhaps with a runtime error report. Jumping out will be considered valid. Jumping in won't be possible. Of course if you want to jump in and out, just avoid using FOR/NEXT, WHILE/WEND, etc. and use only GOTO.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Mar 20, 2021 12:53:20 GMT -5
My age is starting to get tome - I assume 'while dah de dah wend is fine in that it jumps out on its own if the dah de dah is not satisfied? I'll use that unless the for next is running it's full course. I also need to get some help with error handling, but will start a new thread.
I will follow through on the file saving, look at the files in notepad, and practice!
Thank you all very much
|
|