Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CLEAR command
#15
(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.
Reply


Messages In This Thread
CLEAR command - by bartok - 02-02-2024, 10:13 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 02:53 AM
RE: CLEAR command - by bartok - 02-03-2024, 09:52 AM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 04:35 PM
RE: CLEAR command - by bartok - 02-03-2024, 09:11 PM
RE: CLEAR command - by bartok - 02-03-2024, 02:48 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 04:29 PM
RE: CLEAR command - by bartok - 02-03-2024, 06:26 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 05:22 PM
RE: CLEAR command - by bartok - 02-03-2024, 06:16 PM
RE: CLEAR command - by SMcNeill - 02-03-2024, 06:32 PM
RE: CLEAR command - by bartok - 02-03-2024, 08:20 PM
RE: CLEAR command - by SMcNeill - 02-03-2024, 10:24 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 10:55 PM
RE: CLEAR command - by bartok - 02-04-2024, 09:56 AM
RE: CLEAR command - by bplus - 02-03-2024, 06:41 PM
RE: CLEAR command - by TerryRitchie - 02-03-2024, 06:48 PM
RE: CLEAR command - by SMcNeill - 02-04-2024, 12:17 PM
RE: CLEAR command - by bartok - 02-04-2024, 01:13 PM
RE: CLEAR command - by Dimster - 02-04-2024, 03:26 PM
RE: CLEAR command - by bplus - 02-04-2024, 04:16 PM



Users browsing this thread: 3 Guest(s)