Weird string stuff

Anything QL Software or Programming Related.
User avatar
NormanDunbar
Forum Moderator
Posts: 2492
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Weird string stuff

Post 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.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
Andrew
QL Wafer Drive
Posts: 1050
Joined: Tue Jul 17, 2018 9:10 pm

Re: Weird string stuff

Post 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)


User avatar
Artificer
Trump Card
Posts: 168
Joined: Fri Nov 24, 2017 8:43 am

Re: Weird string stuff

Post 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


swensont
Forum Moderator
Posts: 325
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: Weird string stuff

Post 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


User avatar
pjw
QL Wafer Drive
Posts: 1629
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Weird string stuff

Post 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..


Per
I love long walks, especially when they are taken by people who annoy me.
- Fred Allen
User avatar
NormanDunbar
Forum Moderator
Posts: 2492
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: Weird string stuff

Post 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.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
NormanDunbar
Forum Moderator
Posts: 2492
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: Weird string stuff

Post 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.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
NormanDunbar
Forum Moderator
Posts: 2492
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: Weird string stuff

Post 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.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
pjw
QL Wafer Drive
Posts: 1629
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Weird string stuff

Post 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.


Per
I love long walks, especially when they are taken by people who annoy me.
- Fred Allen
Derek_Stewart
Font of All Knowledge
Posts: 4796
Joined: Mon Dec 20, 2010 11:40 am
Location: Sunny Runcorn, Cheshire, UK

Re: Weird string stuff

Post 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.


Regards, Derek
Post Reply