![]() |
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 KingdomPosts: 267 |
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: AustraliaPosts: 6283 |
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 KingdomPosts: 267 |
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. 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. 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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |