(02-03-2024, 10:24 PM)SMcNeill Wrote:(02-03-2024, 08:20 PM)bartok Wrote: Yes, of course, is conceptually very very simple! And for sure is a viable option... but I have to point out that it is simple until you have X(I). But I have a lot of arrays about 2, 3 and even 4 dimensions each. In order to clear them all in that way, I should write a specific long subroutine to do that, that should be modified every time in which I change or add an array, instead to a simple CLEAR. Is would be like to use SPACE$ in order to CLS the screen.
However, I found that CLEAR works. So, I posted my second question (about REDIM before CLEAR) for curiosity. My main doubt was about the first question. However, I am quite astonished that CLEAR is considered so esoteric and forgotten. How a command intended to create the same situation that exists when a program is executed (clearing all variables and arrays) could be secondary?
If you have code which uses 3-dimensional arrays, you have some point in the code where you *must* DIM those arrays. (Unless they're all default 11 element arrays.) When you create them, just pop over to your SUB ResetArray and add them there.
SUB ResetArray
SHARED XYZ(1 to 20, 3 to 50, -17 to 97)
FOR x = LBOUND(XYZ,1) TO UBOUND(XYZ,1) <--- Highlight all this and then CTRL-C to copy it
CTRL-V <--Paste it
CTRL-V <-- Paste it
<-- Edit that X to Y and then Z
.....
You now have:
SUB ResetArray
SHARED XYZ(1 to 20, 3 to 50, -17 to 97)
FOR x = LBOUND(XYZ,1) TO UBOUND(XYZ,1)
FOR Y = LBOUND(XYZ,1) TO UBOUND(XYZ,1)
FOR Z = LBOUND(XYZ,1) TO UBOUND(XYZ,1)
add the following:
XYZ(Z,Y,X) = 0
NEXT Z, Y, X
END SUB
That's it! You are now resetting your 3-dimensional array.
SUB ResetArray
SHARED XYZ(1 to 20, 3 to 50, -17 to 97)
FOR x = LBOUND(XYZ,1) TO UBOUND(XYZ,1)
FOR Y = LBOUND(XYZ,1) TO UBOUND(XYZ,1)
FOR Z = LBOUND(XYZ,1) TO UBOUND(XYZ,1)
XYZ(Z,Y,X) = 0
NEXT Z, Y, X
END SUB
Quick. Simple. Unambigious.
When you add another set of arrays (or even variables which need resetting), then just pop into that SUB and add them as you code. (OR write a second ResetArrayABC command so that you can reset each array independently, as needed, without affecting the other.)
The problem with CLEAR is that it was never designed to work with modern commands. Let me ask you a few questions:
foo = _NEWIMAGE(640,480, 32)
SCREEN foo
CLEAR
Now, in the above scenario, what is supposed to happen? Should the variable foo be cleared and reset to 0? But then, how would one referecne the SCREEN called foo? Should that screen be cleared as well? Or is it memory which is now in use, but with no way to ever _FREEIMAGE it? How about variables/arrays which hold sound/font/images/ect?
How about this:
foo = 123
DIM m AS MEM
m = _MEM(foo)
CLEAR
_MEM was never a part of QB45. Does CLEAR work on it? How's it behave with CLEAR? Does that mem pointer get freed? Reset? Is it lost? Does it point to a block of memory that no longer exists? WTF is happening here?!!??
Does CLEAR work with User Defined Types? STATIC variables? COMMON SHARED variables? STATIC SUB/FUNCTION variables?
???
Honestly, I don't have a clue -- and I've been helping to develop the language here for well over a dozen years now!!
OPEN "temp.txt" FOR OUTPUT AS #foo
CLEAR
What happens with that file above? foo is the variable which references it. Is CLEAR supposed to close that file? Does it remain open, with us having no real way to know how to work with it? What's supposed to happen here? What ACTUALLY happens here???
Again, I don't have a clue.
Before I personally would feel comfortable in using CLEAR in a program, I'd have to sit down for a week and read a manual and test it with dozens of various use cases. Then I'd have to test it for another week with various cases which the manual doesn't bother to cover, as the manual was written before the new cases were ever conceived or thought of. _MEM? Not going to find it in and old QB45 reference books. _LOADIMAGE? _LOADFONT? _SNDOPEN? _EMBEDDED$? Nope. Those didn't exist in any old reference books.
The only way to be ***REALLY*** certain about how it behaves would be to sit down, test every possible case where it could affect something, and then take time to see what's going on. Read the c-output. Study the test results. Check to see exactly where and what and how things are affected....
...and then try and remember all those interactions so you can properly account for them in your own code....
OR....
Just write a quick SUB to clear what you need cleared yourself, without having to worry about corruption coming from anything else.
Ok, I have understood.
For example, I have
DIM matrix!(2, 24, 50, 50)
I can clear it with:
FOR k% = 1 TO 2
FOR z% = 1 TO 24
FOR n% = 1 TO 50
FOR i% = 1 TO 50
matrix!(k%, z%, i%, n%) = 0
NEXT i%, n%, z%, k%
the problem is that I have hundreds of variables and tens of matrixs like the matrix above. Without counting the dynamic arrays. I have so much of this stuff that I'm myself nearly unable to count it! (I'm joking)
About the issues you said with CLEAR, I think to have avoided them. In fact, the CLEAR command is used once on the code. I can't say it is just at the beginning, because it is after the long part of declaration of variables, arrays and types, but in order to be executed more than once (the first time is when the program is launched), the flow of the program always arrives at the end of the main part of the code. As to say, it arrives just "one step" before SYSTEM. At that point, all files have been CLOSEd properly, all images are _FREEIMAGEd, and so on. It is like that beacuse I conceived the code, from the beginning, to calculate all just when the user put the inputs, and then to allow to see the results only of what the user wants to see (if he wants to). So, there is nothing left halfway before the CLEAR command is ativeted. About the other new commands about new kind of variables, I use !, %, %%, &, ~`, which are not new.