Boriel Basic Forum
How to manage part of a STRING? - Printable Version

+- Boriel Basic Forum (https://forum.boriel.com)
+-- Forum: Compilers and Computer Languages (https://forum.boriel.com/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://forum.boriel.com/forumdisplay.php?fid=11)
+---- Forum: How-To & Tutorials (https://forum.boriel.com/forumdisplay.php?fid=13)
+---- Thread: How to manage part of a STRING? (/showthread.php?tid=836)



How to manage part of a STRING? - LukeBord1 - 2018-01-09

In standard Basic, we have this example correctly working:

Code:
10 LET a$="123456789012345678901234567890xy" 20 PRINT AT 0,0; a$ 30 IF INKEY$="0" THEN LET a$=a$(32) + a$(1 TO 31) 40 GOTO 20

...but it doesn't seem to work in ZXB:

Code:
DIM test AS STRING test="123456789012345678901234567890xy" mainloop: PRINT AT 0,0; test IF INKEY="0" THEN test=test(32) + test(1 TO 31) END IF GOTO mainloop

Thoughts?


Re: How to manage part of a STRING? - LukeBord1 - 2018-01-09

SOLVED by myself! :lol:

While in standard Basic the strings starts the count from "1", the ZXB syntax is "zero-based".

All works fine by simply shifting the TO values. Good...


Re: How to manage part of a STRING? - boriel - 2018-01-09

Well done! :!:
There's also another option, --string-base=1 to make the original BASIC code to work.
Also for arrays, --array-base=1

Check command line options for more flags: <!-- m --><a class="postlink" href="http://boriel.com/wiki/en/index.php/ZX_BASIC:Zxb#Command_Line_Options">http://boriel.com/wiki/en/index.php/ZX_ ... ne_Options</a><!-- m -->

Actually, for full compatibility with Sinclair BASIC, you can use --sinclair, which will enable the above plus --strict-boolean (boolean values always 0 or 1).
By default ZX BASIC uses base 0 and boolean values 0 for FALSE, and ANY other as true, because this allows better optimizations and performance.


Re: How to manage part of a STRING? - ShaunBebbers - 2019-01-09

Hello,

I am looking at string manipulation, particularly in terms of deleting the last character from a string.

My routine is as follows:

Code:
3 PRINT "Enter your number"; n$; "c:\>_"; 4 LET a$=INKEY$: IF a$=n$ AND b$<>"" THEN GO TO 9: END IF 5 IF a$="" THEN GO TO 4: END IF 6 IF (CODE a$=8 OR CODE a$=12) AND LEN(b$)>0 THEN LET a$="": GO SUB 2000: GO TO 8: END IF 7 IF CODE a$<48 OR CODE a$>57 THEN GO TO 4: END IF 8 LET b$=b$+a$: PRINT AT 1,4; b$; "_ ": LET a$=INKEY$: GO SUB 1000: GO TO 4 9 REM Rest of programme from here

The clear keyboard buffer routing is as follows:

Code:
1000 IF INKEY$<>"" THEN GO TO 1000:END IF 1001 RETURN

And the "delete last character" routine is:

Code:
2000 LET c$="": 2001 IF LEN(b$)>1 THEN LET c$=b$(0 TO LEN(b$)-1): END IF 2002 LET b$=c$: RETURN

It's the "delete last character" routine that I'm having trouble with. the b$(0 TO LEN(b$)-1) isn't doing what I'm expecting, i.e., if the contents of b$ are:

Code:
12345

I want c$ to be:

Code:
1234

and therefore be passed back to the b$ string before returning.

Many thanks for any help or advice.

Regards,

Shaun.


Re: How to manage part of a STRING? - boriel - 2019-02-07

ShaunBebbers Wrote:Hello,

I am looking at string manipulation, particularly in terms of deleting the last character from a string.

...

It's the "delete last character" routine that I'm having trouble with. the b$(0 TO LEN(b$)-1) isn't doing what I'm expecting, i.e., if the contents of b$ are:

Code:
12345

I want c$ to be:

Code:
1234

and therefore be passed back to the b$ string before returning.

Many thanks for any help or advice.

Regards,

Shaun.

Have you tried with
Code:
b$(0 TO LEN(b$) - 2)
:?:

Since by default ZX Basic strings are 0-based (the standard in BASIC is 1-based, that is, starting from position 1), LEN(b$) - 1 is the position of the last character. So LEN(b$) - 2 is the 2nd to last.

Try this and tell me, please. :roll: