Page 1 of 2
Weird string stuff
Posted: Tue Jan 07, 2025 4:35 pm
by NormanDunbar
Please feel free to give me a good kicking, but I've been programming in C/C++/Assembly/AVR Assembly for far too long, and not enough SuperBASIC.
I have a function that returns a string. It is always returning a null string. I have narrowed it down to:
Code: Select all
DEFine FuNction something$
LOCal buffer$(30), ptr, character$: REM And other stuff, not needed here.
:
ptr = 1
REPeat Loop
REMark Do something to create character$
character$ = doSomething$(x,y,z)
buffer${ptr} = character$
ptr = ptr + 1
:
REMark Some way to exit the loop.
IF something THEN EXIT Loop: END IF
END REPeat Loop
RETurn buffer$
So, it seems that if I DIM or LOCal a string variable, the LENgth of that string is always zero! This shows a similar problem:
Code: Select all
DIM a$(10)
a$(3) = "?"
PRINT a$: REMark gives a NULL string!
PRINT LEN(a$): REMark gives zero!
PRINT a$(3): REMark Gives "?"!!!
What the hell have I forgotten about dimensioned strings?
Cheers,
Norm.
Re: Weird string stuff
Posted: Tue Jan 07, 2025 4:51 pm
by Andrew
Not really a scientific explanation:
DIM will initialise the string with chr$(0).
a$(0) keeps the length of the string - which after DIM is 0
The string length is modified only when you assign a value to the string: a$="1234"
As you only modify one character in string you need to set by hand the length of the string
Code: Select all
DIM a$(10)
a$(3) = "?"
a$(0)=3
PRINT a$
PRINT LEN(a$)
PRINT a$(3)
Re: Weird string stuff
Posted: Tue Jan 07, 2025 5:49 pm
by Artificer
Hi Norm
It seems
DIM a$(10) gives and array of size index 1(0) x 10 characters and the size of the array is returned through PRINT DIMN(a$,(index)).
Buffer$ is an array of only one dimension and needs at least 2.
Cheers
Re: Weird string stuff
Posted: Tue Jan 07, 2025 6:52 pm
by swensont
I look at the issue as this: why do you need to DIM a string?
In C, a string is an array of chars. In SuperBASIC a string is a string.
If you need to get parts of the string, just use a command like x$( 2 to 5) to get the string from the 2nd to 5th characters. Granted when getting the nth character in a string;
let a$ = x$(n)
sort of looks like an array, but it is really string slicing.
Tim
Re: Weird string stuff
Posted: Tue Jan 07, 2025 7:45 pm
by pjw
NormanDunbar wrote: Tue Jan 07, 2025 4:35 pm
<>
I have a function that returns a string. It is always returning a null string. I have narrowed it down to:
<>
Code: Select all
DEFine FuNction something$
LOCal buffer$(30), ptr, character$: REM And other stuff, not needed here.
ptr = 1
REPeat Loop
REMark Do something to create character$
character$ = doSomething$(x,y,z)
buffer$(ptr) = character$
ptr = ptr + 1
REMark Some way to exit the loop.
IF something THEN EXIT Loop: END IF
END REPeat Loop
buffer$(0) = ptr - 1: rem <---- fixes the problem
RETurn buffer$
Scroll to the end..
Re: Weird string stuff
Posted: Tue Jan 07, 2025 8:58 pm
by NormanDunbar
Hi Per.
Thanks. I was aware that character zero was the string length, but surely I've DIMmed or LOCalled the string so tg length should be known already.
Sigh!
Thanks for the reminder.
Cheers,
Norm.
Re: Weird string stuff
Posted: Tue Jan 07, 2025 9:03 pm
by NormanDunbar
Hi Tim.
swensont wrote: Tue Jan 07, 2025 6:52 pm
I look at the issue as this: why do you need to DIM a string?
My code was a minimal example of the problem. I'm dimensioning the string to avoid having to keep extending it as I add characters. Ing, as you know, preallocated enough space for the whole thing. Also advised for code that will be compiled by Turbo.
It's a long string, potentially, but longer than the one I showed in the example. Also, I'm adding characters to the string, not reading them back. Once the function returns, I'll be using the resulting string in it's entirety, not in slices.
Thanks for your reply.
Cheers,
Norm.
Re: Weird string stuff
Posted: Tue Jan 07, 2025 9:09 pm
by NormanDunbar
Dilwyn and Andrew, thanks for your replies.
I can either assign the desired length to the zeroth character as suggested but Per, or prefill the dimensioned string with spaces instead of the default CHR$(0).
Cheers,
Norm.
Re: Weird string stuff
Posted: Tue Jan 07, 2025 10:43 pm
by pjw
I struggled a bit with this phenomenon in a slightly different context:
Code: Select all
dim bytes$(10)
cf = fop_in(<some file>)
bytes$(0) = 10
bget#cf; bytes$
..
Yup, thats the way to do bget 10 bytes, else you get nothing! Weird, but on
closer inspection, logical, I suppose. The SBASIC keywords manual was of no
help though.
Re: Weird string stuff
Posted: Mon Jan 13, 2025 12:31 pm
by Derek_Stewart
Hi,
I have been reading, Artificial Intelligenence on the QL, after I made single page and OCR.
Texamples in the book and the programme at the end of the of the book "Computer_SalesMan_bas", use arrays to store the data and use array element 0 as a data element, string or integer.
Consulting many QL manuals and books, they say that the length of the string is the last element in the array, as detailed in the QL User Guide and many books.
Only the Online Supebasic Manual, The Definitive Superbasic Guide by Jan Jones, gives array element 0 as the lenth of the string.
This has I expected, but thd more interesting question is why are so many manuals and books including the QL User Guide saying incorrectly that the length of the array is held at the end of the array.
That said how many programmes do not work in SMSQ/E or Minerva based on incorrect asumpions on the array storage details.
Needless to say, the programme: "Computer_Saleman_bas" needs correcti g.
I think I should stick to assembler programming, it is alot easier.