Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Declaring the length of a string
#1
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"
20 DIM b$(8)
30 LET b$=a$
40 LET b$(4 TO 8)="p me!"
50 PRINT b$

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$
a$="hello"
b$=a$
b$(3 TO 4)="p "
b$=b$+"me!"
PRINT b$

Is this expected? Or is there a better way of building b$ from a$ in this example in only one step?
Reply
#2
(01-03-2021, 01:55 AM)patters Wrote: 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:
...

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$
a$="hello"
b$=a$
b$(3 TO 4)="p "
b$=b$+"me!"
PRINT b$

Is this expected? Or is there a better way of building b$ from a$ in this example in only one step?

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$
a$="hello"
b$=a$(TO 2) + "p me!"
PRINT b$

Also, if you're using Sinclair BASIC features, there is a compatibility flag, --sinclair which will do the following:
  • Arrays will start at position 1 by default, not 0
  • Strings will start at position 1, not 0
  • Booleans are always 0 or 1 (otherwise they will be 0 = False, Any other value = True)
  • Will include extra functions (ATTR, POINT), that won't be compiled if not used.
Keep that in mind. The above increases compatibility, but performance could be slightly slower (minimally) and will take a bit more memory (some bytes more).
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.
Reply
#3
(01-03-2021, 11:01 AM)boriel Wrote: You can do it all in a single sentence in most cases:
Code:
DIM a$, b$
a$="hello"
b$=a$(TO 2) + "p me!"
PRINT b$

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! Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)