Author Topic: On Clearing a STATIC array  (Read 3223 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
On Clearing a STATIC array
« on: October 07, 2021, 04:09:14 pm »
Should Erase work for clearing values in a STATIC array? I tried it here
https://www.qb64.org/forum/index.php?topic=4266.msg136545#msg136545

Got errors, so I ended up doing it the hard way ie, resetting each and every value back to 0 in 2 index For loops for 2D arrays. I am wondering if I just screwed up with Erase.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: On Clearing a STATIC array
« Reply #1 on: October 07, 2021, 04:32:12 pm »
Try ERASE

Code: QB64: [Select]
  1. DIM x$(100)
  2. x$(11) = "Steve"
  3. PRINT x$(11)
  4. PRINT x$(11); " Hey, where'd Steve go?"
  5.  

But I get it now... I know it works with DIM, but not REDIM. Also, I just looked at your post, tried this experiment, and it fails. Now I see your problem...

Code: QB64: [Select]
  1. CALL pete
  2. SUB pete
  3.     STATIC x$(1 TO 100)
  4.     x$(11) = "Steve"
  5.     PRINT x$(11)
  6.     ERASE x$
  7.     PRINT x$(11); " Hey, where'd Steve go?"

So now I'm wondering how ERASE worked in QuickBASIC?

Okay, for a QBasic page, I get this...

For static arrays, ERASE sets each element of a numeric array to zero and each element of a string array to null.
For dynamic arrays, ERASE frees the memory used by the array. You must redeclare the array's dimensions with REDIM or DIM before using it.

So maybe this is a QB64 bug. Sorry, I don't have QBasic on this machine.

Pete


« Last Edit: October 07, 2021, 04:51:49 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: On Clearing a STATIC array
« Reply #2 on: October 07, 2021, 05:12:40 pm »
Thanks Pete an alternate illustration of my Point.


Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: On Clearing a STATIC array
« Reply #3 on: October 07, 2021, 05:25:24 pm »
Ah, in QBasic, we were not able to code: STATIC x$(1 to 100)

QB only accepted: STATIC x$()

So if I were coding this in QB, I'd have to do the following to zero my arrays and reuse them...

Code: QB64: [Select]
  1.     CALL pete
  2.     SUB pete
  3.         STATIC x$()
  4.         DIM x$(1 TO 100)
  5.         x$(11) = "Steve"
  6.         PRINT x$(11)
  7.         REDIM x$(1 to 100) ' Sets all elements to null.
  8.         PRINT x$(11); " Hey, where'd Steve go?"
  9.     END SUB

So you could use ERASE this way, but you'd still have to do the REDIM. It turns out in QB ERASE does not preserve the array bounds. QB64 does the same, so not a bug. So I would just REDIM and forget about ERASE, because you intend to use the array again. Is this what you are looking for?

Pete

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: On Clearing a STATIC array
« Reply #4 on: October 07, 2021, 05:36:35 pm »
STATIC X(1000)
DIM M AS _MEM: M = _MEM(X())

For I = 0 to 1000
   X(i) = i
NEXT

_MEMFILL M, M.OFFSET, M.SIZE, 0 AS _BYTE ‘reset the array

FOR I = 0 to 1000
    Print X(I)
Next
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: On Clearing a STATIC array
« Reply #5 on: October 07, 2021, 05:47:03 pm »
@SMcNeill

Which is the same as...

Code: QB64: [Select]
  1. CALL pete
  2. SUB pete
  3.     STATIC X(1000)
  4.     FOR I = 0 TO 1000
  5.         X(I) = I
  6.     NEXT
  7.  
  8.     FOR I = 0 TO 1000
  9.         X(I) = 0
  10.         PRINT X(I)
  11.     NEXT
  12.  

Right?

or...

Code: QB64: [Select]
  1. CALL pete
  2. SUB pete
  3.     STATIC X(1000)
  4.     FOR I = 0 TO 1000
  5.         X(I) = I
  6.     NEXT
  7.  
  8.     REDIM x(1000)
  9.  
  10.     FOR I = 0 TO 1000
  11.             PRINT X(I)
  12.     NEXT
  13.  

Right?

BTW - I posted about a related inconsistency between QB and QB64. Have a look, if you have the time.

Pete

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: On Clearing a STATIC array
« Reply #6 on: October 07, 2021, 06:22:14 pm »
It is.  It's just a faster and easier way to do the same thing.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: On Clearing a STATIC array
« Reply #7 on: October 07, 2021, 10:28:09 pm »
STATIC X(1000)
DIM M AS _MEM: M = _MEM(X())

For I = 0 to 1000
   X(i) = i
NEXT

_MEMFILL M, M.OFFSET, M.SIZE, 0 AS _BYTE ‘reset the array

FOR I = 0 to 1000
    Print X(I)
Next

This assumes default Single Type, correct?

Or this might figure the Return according to X() Type
M = _MEM(X())

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: On Clearing a STATIC array
« Reply #8 on: October 07, 2021, 11:39:44 pm »
This assumes default Single Type, correct?

Or this might figure the Return according to X() Type
M = _MEM(X())

Any type but strings.  _MEM won't work with variable length strings, and if you're using fixed length, you'd probably want to fill with 32 instead of 0.  (Spaces CHR$(32) rather than NULL CHR$(0).)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!