{"id":5508,"date":"2023-11-20T16:44:37","date_gmt":"2023-11-20T15:44:37","guid":{"rendered":"https:\/\/msxitalia.org\/?p=5508"},"modified":"2023-11-20T16:44:37","modified_gmt":"2023-11-20T15:44:37","slug":"msx-basic-lezione-14","status":"publish","type":"post","link":"https:\/\/msxitalia.org\/en\/msx-basic-lezione-14\/","title":{"rendered":"MSX BASIC \u2013 lesson 14"},"content":{"rendered":"<p class=\"wp-block-paragraph\"><img decoding=\"async\" data-src=\"https:\/\/msxitalia.org\/wp-content\/uploads\/2023\/11\/Corso-Basic-MSX1-OC-300x225.jpg\" class=\"size-medium wp-image-5338 aligncenter lazyload\" src=\"data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\" alt=\"\" width=\"300\" height=\"225\" \/><noscript><img decoding=\"async\" class=\"size-medium wp-image-5338 aligncenter\" src=\"https:\/\/msxitalia.org\/wp-content\/uploads\/2023\/11\/Corso-Basic-MSX1-OC-300x225.jpg\" alt=\"\" width=\"300\" height=\"225\"><\/noscript><\/p>\r\n<h1 dir=\"auto\" style=\"text-align: center;\">\u201cMSX BASIC\u201d COLUMN by Orazio Cacciola (part 14)<\/h1>\r\n<h2 style=\"text-align: center;\">Functions for working with strings (2nd part)<\/h2>\r\n<p>Let&#039;s continue with the other functions, after this episode, there will be no more secrets to manipulate our strings.<br \/><br \/><strong>STR$<\/strong><br \/>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$=\u201d-10\u2033. 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.<br \/>Syntax: STR$(Numeric Variable)<br \/><br \/><strong>LEFT$<\/strong><br \/>Returns a string determined by the value that was inserted into the function, reading from left to right. PRINT LEFT$(\u201cHello everyone\u201d,4), will print the first 4 characters on the screen, namely: Hello. We can also use a string variable: C$=\u201dHello everyone\u201d: 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 &quot;Hello&quot;.<br \/>Syntax: LEFT$(Alphanumeric Variable, Number of Characters). <br \/><br \/><strong>RIGHT$<\/strong><br \/>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$=\u201dHello everyone\u201d: PRINT RIGHT$(C$,5), will print the phrase \u201ceveryone\u201d. 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.<br \/>Syntax: RIGHT$(Alphanumeric Variable, Number of Characters)<br \/><br \/><strong>MID$<\/strong><br \/>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&#039;s look at some examples to understand better. Let&#039;s start copying one part of the string into another. A$=\u201dORAZIO AND ANDREA\u201d: 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$=\u201dSPOOTER CAR\u201d: MID$(A$,1,4)=\u201dMOTORCYCLE\u201d. The variable A$ which previously had stored the word AUTOPATTERN, after modification with MID$ became PATTERN MOTORCYCLE. Let&#039;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.<\/p>\r\n<pre style=\"padding-left: 40px;\">10 INPUT &quot;Enter the phrase&quot;; F$: CLS<br \/>20 INPUT &quot;Insert the letter&quot;; L$<br \/>30 L=LEN(F$): FOR N=1 TO L<br \/>40 IF MID$(F$,I,1)=L$ THEN PRINT &quot;GOOD YOU GUESSED IT&quot;: END<br \/>50 NEXT N: PRINT &quot;I&#039;M SORRY, YOU LOST&quot;: END<\/pre>\r\n<p>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.<br \/>We can also use the variable first: A$=\u201dMOTORCYCLE\u201d:B$=MID$(A$,1,4). The B$ will be \u201cMOTO\u201d.<br \/>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.<br \/>Syntax: MID$(Variable$, Starting value, Quantity of characters)<br \/><br \/>Thank you all. Until next time<\/p>","protected":false},"excerpt":{"rendered":"<p>RUBRICA &#8220;MSX BASIC&#8221; a cura di Orazio Cacciola (parte 14) Funzioni per lavorare con le stringhe (2\u00b0parte) Riprendiamo con le altre funzioni, dopo questa puntata, non ci saranno pi\u00f9 segreti per manipolare le nostre stringhe. STR$Restituisce una stringa da un valore numerico. Pu\u00f2 essere stampata direttamente sullo schermo o memorizzata in una variabile alfanumerica: PRINT [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[14],"tags":[],"dipi_cpt_category":[],"class_list":["post-5508","post","type-post","status-publish","format-standard","hentry","category-msx-basic"],"_links":{"self":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts\/5508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/comments?post=5508"}],"version-history":[{"count":1,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts\/5508\/revisions"}],"predecessor-version":[{"id":5509,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/posts\/5508\/revisions\/5509"}],"wp:attachment":[{"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/media?parent=5508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/categories?post=5508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/tags?post=5508"},{"taxonomy":"dipi_cpt_category","embeddable":true,"href":"https:\/\/msxitalia.org\/en\/wp-json\/wp\/v2\/dipi_cpt_category?post=5508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}