LEFT$() and RIGHT$() assignment

This forum is dedicated to those who wish to suggest new features.
Questo forum è dedicato a chi desidera suggerire nuove feature.
Ce forum est dédié à ceux qui souhaitent suggérer de nouvelles fonctionnalités.
Post Reply
spotlessmind1975
Site Admin
Posts: 163
Joined: Fri Oct 06, 2023 8:25 pm

LEFT$() and RIGHT$() assignment

Post 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$(..).
User avatar
wysardry
Posts: 26
Joined: Sun Oct 08, 2023 6:45 pm

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

Post 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"
Pwillard
Posts: 2
Joined: Tue Oct 10, 2023 1:15 pm

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

Post 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.
ldir_hector
Posts: 12
Joined: Sun Oct 08, 2023 11:47 am

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

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