Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:12 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: REDIM workarounds for STRINGS and maybe Arrays

Author Message
Womble

Senior Member

Joined: 09/07/2020
Location: United Kingdom
Posts: 267
Posted: 03:02am 29 Oct 2020
Copy link to clipboard 
Print this post

This thread discussed the Qbasic REDIM command which is not implemented in MMBasic.

Peter posted details there from the forthcoming manual with the ERASE command to free up variables and arrays.

I have not tried this with an array or any other variable types,
but it works for STRINGS.

Here is my code experiment:
'------------------------------------------------
' Redimentioning Strings experiment by Womble
'------------------------------------------------

OPTION BASE 1           ' arrays start at 1

'------------------------------------------------
' OPTION EXPLICIT        DO NOT use this
'------------------------------------------------

DIM FLOAT sl = 10
DIM s$ LENGTH 10 = STRING$(sl,35)   'String of #
DIM FLOAT i

PRINT "Initail s$ length = " LEN(s$) " " s$
sl = LEN(s$)

FOR i = 1 to 3
  sl = 10 * i
  REDIMSTR sl
  PRINT "s$ length = " LEN(s$) " " s$
NEXT i

FOR i = 3 to 1 STEP -1
  sl = 10 * i
  REDIMSTR sl
  PRINT "s$ length = " LEN(s$) " " s$
NEXT i

END

'------------------------------------------------

SUB REDIMSTR sl
  LOCAL FLOAT strsize = sl

  ' This only works because the string is Global
  ERASE s$
  DIM s$ LENGTH strsize = STRING$(strsize,35) '#

END SUB

'------------------------------------------------

Works fine for a Global STRING,
but as per the manual Do Not Use OPTION EXPLICIT.

My thoughts were that this could be used to emulate the REDIM command from GwBasic/QBasic at least to some extent.

Regards

Womble
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 04:20am 29 Oct 2020
Copy link to clipboard 
Print this post

Simple strings always use the full 255+1 byes of storage so there is no advantage to using LENGTH to save memory.
It is only with arrays of strings that there is any advantage.

To REDIM, you would have to create the new array then copy the elements over then erase the original then rename the new array by doing the same again.

You could possibly reduce the size of an array by POKING the memory but you would probably end up with dead space so no advantage there either.


Jim
VK7JH
MMedit
 
Womble

Senior Member

Joined: 09/07/2020
Location: United Kingdom
Posts: 267
Posted: 12:49pm 29 Oct 2020
Copy link to clipboard 
Print this post

  TassyJim said  Simple strings always use the full 255+1 byes of storage so there is no advantage to using LENGTH to save memory.
It is only with arrays of strings that there is any advantage.

Jim ...

You are quite correct.  I was not thinking about memory usage, we have plenty of that on the CMM2.
I had also missed the crucial sentence on the following manual page that explicitly stated that simple strings were a fixed size.

I was more concerned with LEN()
although on reflection using a STRING for my "proof of concept/I wonder how this works" experiment was probably not a good choice.
An array would have been more useful.

  TassyJim said  To REDIM, you would have to create the new array then copy the elements over then erase the original then rename the new array by doing the same again.

I appreciate that.  
I was more thinking of the contents of a buffer string containing an arbitrary length string of characters and copying the contents into a new string of known length.  Both strings having been already declared in my program.
Efficiency was not uppermost in my mind.
I simply wanted to see how ERASE worked given that it is missing from the "current" manual.

  TassyJim said  You could possibly reduce the size of an array by POKING the memory but you would probably end up with dead space so no advantage there either.

I have no idea how you would do this.  If I tried something like that it would lead to tears.
Not messing with PEEK and POKE until I have brushed up alot more with my Rusty Basic skills (or lackof).

Many thanks for your comments.  More food for thought.  Appreciated.

Womble
Edited 2020-10-29 22:55 by Womble
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025