![]() |
Declaring the length of a string - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16) +---- Thread: Declaring the length of a string (/showthread.php?tid=1002) |
Declaring the length of a string - patters - 01-03-2021 In Sinclair BASIC you can DIM the length of a string using DIM a$(5) If I try something similar in Boriel BASIC DIM a$(5) AS STRING the compiler seems to interpret this as an array declaration. Sometimes it's useful to declare the largest expected length of a string if you want to build it up of component pieces. e.g. In Sinclair BASIC: Code: 10 DIM a$="hello" If I try to do this in Boriel BASIC I can't declare the length, nor does it expand when needed. I seem to have to do it in two steps, one for overwriting the parts of the existing string, and a separate step to glue an addition to the end of the string. Code: DIM a$, b$ Is this expected? Or is there a better way of building b$ from a$ in this example in only one step? RE: Declaring the length of a string - boriel - 01-03-2021 (01-03-2021, 01:55 AM)patters Wrote: In Sinclair BASIC you can DIM the length of a string using This is expected. For speed reasons, DIM is always used for ARRAYS. There's no way to limit string length in ZXBasic. Updating a substring is done like in Sinclair BASIC ("procrustean method"), so the length of the string does not change. You can do it all in a single sentence in most cases: Code: DIM a$, b$ Also, if you're using Sinclair BASIC features, there is a compatibility flag, --sinclair which will do the following:
I suggest you to use these if you're starting with ZX Basic until you're familiarized with it. READ, DATA and RESTORE are available too. RE: Declaring the length of a string - patters - 01-03-2021 (01-03-2021, 11:01 AM)boriel Wrote: You can do it all in a single sentence in most cases: Ah yes, that's what I was looking for thanks. It was a bit late, I couldn't get my head around it. Thanks, again! ![]() |