MSX BASIC – lesson 14

da | Nov 20, 2023 | MSX BASIC | 0 comments

“MSX BASIC” COLUMN by Orazio Cacciola (part 14)

Functions for working with strings (2nd part)

Let's continue with the other functions, after this episode, there will be no more secrets to manipulate our strings.

STR$
Returns a string from a numeric value. It can be printed directly on the screen or stored in an alphanumeric variable: PRINT STR$(10) or A$=STR$(10). In both cases a 3 character string will be printed or stored. This happens because a positive or negative numeric value always has a plus sign, but when it is a positive value the plus sign (+) is omitted, leaving a blank space. If instead I use: A$=STR$(-10), the negative sign(-) is displayed and there will be no blank space in the A$ variable: A$=”-10″. As mentioned above we can also use numerical variables: A=150: A$=STR$(A). In this other case the characters inside the variable A$ are 4.
Syntax: STR$(Numeric Variable)

LEFT$
Returns a string determined by the value that was inserted into the function, reading from left to right. PRINT LEFT$(“Hello everyone”,4), will print the first 4 characters on the screen, namely: Hello. We can also use a string variable: C$=”Hello everyone”: PRINT LEFT$(C$,4), will do the same thing as above. The number of characters in the second argument must not exceed 255. If, still talking about the second argument, it exceeds the number of characters in the string you are searching for, no error will be reported, the entire string will be printed and nothing more. It can also be saved in an alphanumeric variable: A$=LEFT$(C$,4). The variable A$ has stored the first 4 characters of the C$, namely the word "Hello".
Syntax: LEFT$(Alphanumeric Variable, Number of Characters).

RIGHT$
It has the same capabilities as the LEFT$ function, the only point that distinguishes them is that it reads from right to left. Using the example above: C$=”Hello everyone”: PRINT RIGHT$(C$,5), will print the phrase “everyone”. The word will be printed as it is in the C$ variable and not backwards. It reads from right to left but without changing the sentence.
Syntax: RIGHT$(Alphanumeric Variable, Number of Characters)

MID$
It is one of the most powerful commands for string manipulation. It can copy, modify, save or search within a string, that is, everything you can think of is possible. It has three parameters; the first is the alphanumeric variable or string where you want to work, the second is a numerical value or variable to know from which position the search should start and finally the third, always a numerical value or variable, how many characters are to be searched for or stored , if the latter is omitted, all remaining characters of the string will be stored. Let's look at some examples to understand better. Let's start copying one part of the string into another. A$=”ORAZIO AND ANDREA”: MID$(A$,1,6)=B$. We extracted the name ORAZIO from the variable A$ and copied it into the variable B$. In fact, we told him to take the first 6 characters of the variable A$ and copy them into B$. With this other example we will modify a string. A$=”SPOOTER CAR”: MID$(A$,1,4)=”MOTORCYCLE”. The variable A$ which previously had stored the word AUTOPATTERN, after modification with MID$ became PATTERN MOTORCYCLE. Let's say we want to create a game with words, guess the alphabetical letter. A second person will enter a word and you will have to guess if the letter you chose is inside.

10 INPUT "Enter the phrase"; F$: CLS
20 INPUT "Insert the letter"; L$
30 L=LEN(F$): FOR N=1 TO L
40 IF MID$(F$,I,1)=L$ THEN PRINT "GOOD YOU GUESSED IT": END
50 NEXT N: PRINT "I'M SORRY, YOU LOST": END

After entering the sentence and letter in lines 10 and 20, not knowing the length of the sentence, we will find it with the LEN function. Knowing this, we start the loop by scanning one character at a time. In fact, in line 40 the IF will check the variable F$ with L$ character by character via MID$. If one of these is the same, it will print the message of the same line, otherwise the end of cycle message, line 50.
We can also use the variable first: A$=”MOTORCYCLE”:B$=MID$(A$,1,4). The B$ will be “MOTO”.
The MID$ will not report error messages if the length of the string is exceeded, in both cases, both when starting the search and the number of characters to print or store, it will save or print empty strings. But it will report the error if it exceeds 255 in either case, which is the maximum number of string storage.
Syntax: MID$(Variable$, Starting value, Quantity of characters)

Thank you all. Until next time

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 26 – INTERVAL ON, RND AND OTHERS

Good morning everyone, In this episode I will talk about an important interrupt, especially for games, when we want to make an enemy suddenly appear or a useful object, but also to create a stopwatch or a clock and many other things. The COMMAND exploits the...

LEZIONE 26 – INTERVAL ON, RND ED ALTRI

LESSON 24 – THE DATA MANAGED BY THE MSX (PART 3)

Kind regards to everyone. Today we will close this topic, I will talk about direct or random access files. But first, as I mentioned in the last episode, I will give a small explanation about the program. First of all, the first thing to say is that I used a command which...