Page 1 of 1

LEFT$() and RIGHT$() assignment

Posted: Mon Oct 09, 2023 5:40 pm
by spotlessmind1975
hello everyone, and welcome!

I'm trying to tidy up the manual before releasing the new version of ugBASIC (v1.15) and I found myself faced with a rather questionable implementation of the LEFT$(..) and RIGHT$(..) commands.

I'll try to explain better.

In general, BASIC has facilities for extracting substrings from a string. For example,

Code: Select all

a$ = LEFT$("pippo", 3) : PRINT a$: REM prints "pip"
However, some BASIC (e.g. AMOS) also has a LEFT instruction:

Code: Select all

a$ = "punto": LEFT$(a$, 3) = "plu": PRINT a$: REM prints "pluto"
Now, it is clear that some limits must be set. But which ones? For example, if in the previous example the string to be replaced was more than 3 characters long, what should ugBASIC do? What if the string a$ is shorter than 3 characters? I need some advice on smart implementation.

For those who are curious... to date the assignment only works if the string to be replaced is as long as the numeric parameter of LEFT$(..) / RIGHT$(..).

Re: LEFT$() and RIGHT$() assignment

Posted: Tue Oct 10, 2023 12:37 am
by wysardry
I don't really like the idea of using LEFT$ as a way to replace characters in a string. I don't remember it being a feature on any of the 8-bit machines I used.

Would this also apply to RIGHT$ and MID$?

I would expect the compiler to lengthen or shorten a string to accomodate replacing 3 characters with a different number of characters and also to prevent any sort of overflow at runtime, even if that meant truncating the string.

Do you have plans for some sort of REPLACE function? One that would replace all occurances of a substring within a string with another substring, I mean.

Something like:-

Code: Select all

a$ = "punto punto": REPLACE$(a$, "un", "lu"): PRINT a$: REM prints "pluto pluto"

Re: LEFT$() and RIGHT$() assignment

Posted: Mon Oct 16, 2023 11:58 am
by Pwillard
I tend to agree... A REPLACE command (like Visual Basic) would be more sensible as I don't recall ever using LEFT$ and RIGHT$ this way on my COCO. Even a SWAP command would work, I suppose.

GWBASIC supposedly could do left-side replacement though.

Re: LEFT$() and RIGHT$() assignment

Posted: Mon Oct 16, 2023 7:19 pm
by ldir_hector
On the Amstrad CPC, the instruction to extract characters from any position in the string was INSTR$ and it could also be used to replace characters inside the string. I think a replace function would be much more readable.