Ml functions: Difference between revisions
No edit summary |
No edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 5: | Line 5: | ||
All ML functions begin with an "@" followed by one or two digits (the function number). Functions are either STRING or NUMERIC, as indicated in the table. Some functions accept required and/or optional parameters, which are placed in parentheses after the function number. | All ML functions begin with an "@" followed by one or two digits (the function number). Functions are either STRING or NUMERIC, as indicated in the table. Some functions accept required and/or optional parameters, which are placed in parentheses after the function number. | ||
There are 22 string-returning functions & 12 numeric-returning functions. | |||
Example: | Example: | ||
| Line 26: | Line 28: | ||
|- | |- | ||
| @0 | | @0 | ||
| <strong>Input Buffer | | <strong>Resident Input Buffer</strong><br> | ||
<strong>Format: @0 : STRING</strong><br> | <strong>Format: @0 : STRING</strong><br> | ||
Equivalent to <code>LEFT$(TX$,!40)</code>. Commonly used after disk input (.01) or keyed input (.02) when the input buffer is TX$ and the character count is in !40.<br> | Equivalent to <code>LEFT$(TX$,!40)</code>. Commonly used after disk input (.01) or keyed input (.02) when the input buffer is TX$ and the character count is in !40.<br> | ||
| Line 32: | Line 34: | ||
|- | |- | ||
| @1 | | @1 | ||
| <strong> | | <strong>Number to String Without Leading Space</strong><br> | ||
<strong>Format: @1( <number> ) : STRING</strong><br> | <strong>Format: @1( <number> ) : STRING</strong><br> | ||
Returns the equivalent of <code>STR$(<number>)</code> without the leading blank for positive numbers. Negative values retain the minus sign.<br> | Returns the equivalent of <code>STR$(<number>)</code> without the leading blank for positive numbers. Negative values retain the minus sign.<br> | ||
| Line 40: | Line 42: | ||
|- | |- | ||
| @2 | | @2 | ||
| <strong> | | <strong>Forward Substring Search</strong><br> | ||
<strong>Format: @2( <string1> , <string2> [ , <number> ] ) : NUMERIC</strong><br> | <strong>Format: @2( <string1> , <string2> [ , <number> ] ) : NUMERIC</strong><br> | ||
Searches for <string1> within <string2> from left to right and returns the 1-based position. Returns 0 if not found.<br> | Searches for <string1> within <string2> from left to right and returns the 1-based position. Returns 0 if not found.<br> | ||
| Line 57: | Line 59: | ||
|- | |- | ||
| @3 | | @3 | ||
| <strong>Repeat | | <strong>Repeat Character</strong><br> | ||
<strong>Format: @3( <string> , <number> ) : STRING</strong><br> | <strong>Format: @3( <string> , <number> ) : STRING</strong><br> | ||
Returns a string made of the first character of <string> repeated <number> times.<br> | Returns a string made of the first character of <string> repeated <number> times.<br> | ||
| Line 64: | Line 66: | ||
|- | |- | ||
| @4 | | @4 | ||
| <strong> | | <strong>Character Input</strong><br> | ||
<strong>Format: @4 : STRING</strong><br> | <strong>Format: @4 : STRING</strong><br> | ||
Input character (GET-style). Returns one character if available; otherwise returns a null string.<br> | |||
Example: <strong>A$=@4</strong> | Example: <strong>A$=@4</strong> | ||
|- | |- | ||
| @5 | | @5 | ||
| <strong>Read Disk | | <strong>Read Disk Record</strong><br> | ||
<strong>Format: @5 : STRING</strong><br> | <strong>Format: @5 : STRING</strong><br> | ||
Performs a disk line read (.01) and returns the resulting line as a string. Similar to @0, but executes the read first.<br> | Performs a disk line read (.01) and returns the resulting line as a string. Similar to @0, but executes the read first.<br> | ||
| Line 76: | Line 78: | ||
|- | |- | ||
| @6 | | @6 | ||
| <strong> | | <strong>Byte or Word Extraction</strong><br> | ||
<strong>Format: @6( <string1> [ , <string2> ] ) : NUMERIC</strong><br> | <strong>Format: @6( <string1> [ , <string2> ] ) : NUMERIC</strong><br> | ||
Extended ASC for 8-bit and 16-bit extraction. Behavior depends on argument form:<br> | Extended ASC for 8-bit and 16-bit extraction. Behavior depends on argument form:<br> | ||
| Line 87: | Line 89: | ||
|- | |- | ||
| @7 | | @7 | ||
| <strong> | | <strong>Word to Two-Byte String</strong><br> | ||
<strong>Format: @7( <number> ) : STRING</strong><br> | <strong>Format: @7( <number> ) : STRING</strong><br> | ||
Extended CHR$. Returns a 2-character string containing the low byte then high byte of <number>. Useful for relative file positioning.<br> | Extended CHR$. Returns a 2-character string containing the low byte then high byte of <number>. Useful for relative file positioning.<br> | ||
| Line 93: | Line 95: | ||
|- | |- | ||
| @8 | | @8 | ||
| <strong> | | <strong>Interactive Line Input</strong><br> | ||
<strong>Format: @8 : STRING</strong><br> | <strong>Format: @8 : STRING</strong><br> | ||
Performs keyed line input (.02) and returns the resulting line as a string.<br> | Performs keyed line input (.02) and returns the resulting line as a string.<br> | ||
| Line 99: | Line 101: | ||
|- | |- | ||
| @9 | | @9 | ||
| <strong> | | <strong>Free Memory Without Garbage Collection</strong><br> | ||
<strong>Format: @9 : NUMERIC</strong><br> | <strong>Format: @9 : NUMERIC</strong><br> | ||
Enhanced FRE(). Returns free memory as a positive value and does not force garbage collection; uses an internal calculation routine.<br> | Enhanced FRE(). Returns free memory as a positive value and does not force garbage collection; uses an internal calculation routine.<br> | ||
| Line 105: | Line 107: | ||
|- | |- | ||
| @10 | | @10 | ||
| <strong>Strip Control | | <strong>Strip Control and Graphics Characters</strong><br> | ||
<strong>Format: @10( <string> [ , <number> ] ) : STRING</strong><br> | <strong>Format: @10( <string> [ , <number> ] ) : STRING</strong><br> | ||
Returns <string> with graphics/control characters removed. Optional <number> returns only the first <number> characters (LEFT$-style).<br> | Returns <string> with graphics/control characters removed. Optional <number> returns only the first <number> characters (LEFT$-style).<br> | ||
| Line 112: | Line 114: | ||
|- | |- | ||
| @11 | | @11 | ||
| <strong> | | <strong>Current Time String</strong><br> | ||
<strong>Format: @11 : STRING</strong><br> | <strong>Format: @11 : STRING</strong><br> | ||
Returns current time as "HH:MM am" or "HH:MM pm". Updates !45 (hour) and !44 (AM/PM flag: 0=AM, non-zero=PM). | Returns current time as "HH:MM am" or "HH:MM pm". Updates !45 (hour) and !44 (AM/PM flag: 0=AM, non-zero=PM). | ||
|- | |- | ||
| @12 | | @12 | ||
| <strong>Overlay String | | <strong>Overlay String at Position</strong><br> | ||
<strong>Format: @12( <string1> , <string2> , <number> ) : STRING</strong><br> | <strong>Format: @12( <string1> , <string2> , <number> ) : STRING</strong><br> | ||
Overlays <string1> onto <string2> starting at position <number> (1–255), replacing existing characters. Pads with spaces if needed.<br> | Overlays <string1> onto <string2> starting at position <number> (1–255), replacing existing characters. Pads with spaces if needed.<br> | ||
| Line 130: | Line 132: | ||
|- | |- | ||
| @13 | | @13 | ||
| <strong> | | <strong>Crash Error Message</strong><br> | ||
<strong>Format: @13 : STRING</strong><br> | <strong>Format: @13 : STRING</strong><br> | ||
Crash routine helper: returns the BASIC error message text associated with the crash. | Crash routine helper: returns the BASIC error message text associated with the crash. | ||
|- | |- | ||
| @14 | | @14 | ||
| <strong> | | <strong>Crash Error Line Number</strong><br> | ||
<strong>Format: @14 : STRING</strong><br> | <strong>Format: @14 : STRING</strong><br> | ||
Crash routine helper: returns the BASIC error line number as a string. | Crash routine helper: returns the BASIC error line number as a string. | ||
|- | |- | ||
| @15 | | @15 | ||
| <strong> | | <strong>Date to Absolute Day Number</strong><br> | ||
<strong>Format: @15( <year> , <month> , <day> ) : NUMERIC</strong><br> | <strong>Format: @15( <year> , <month> , <day> ) : NUMERIC</strong><br> | ||
Returns an ADN (absolute day number) from year/month/day. Returns -1 if any parameter is invalid. | Returns an ADN (absolute day number) from year/month/day. Returns -1 if any parameter is invalid. | ||
|- | |- | ||
| @16 | | @16 | ||
| <strong> | | <strong>Absolute Day Number to Year (Sets Month/Day)</strong><br> | ||
<strong>Format: @16( <number> ) : NUMERIC</strong><br> | <strong>Format: @16( <number> ) : NUMERIC</strong><br> | ||
Converts ADN in <number> to a date and returns the year. Updates !43 (month) and !42 (day). Invalid ADN yields undefined results. | Converts ADN in <number> to a date and returns the year. Updates !43 (month) and !42 (day). Invalid ADN yields undefined results. | ||
|- | |- | ||
| @17 | | @17 | ||
| <strong> | | <strong>Absolute Day Number to Day of Week</strong><br> | ||
<strong>Format: @17( <number> ) : NUMERIC</strong><br> | <strong>Format: @17( <number> ) : NUMERIC</strong><br> | ||
Returns day-of-week from ADN in <number>: 0–6 for Sunday–Saturday. | Returns day-of-week from ADN in <number>: 0–6 for Sunday–Saturday. | ||
|- | |- | ||
| @18 | | @18 | ||
| <strong> | | <strong>Absolute Day Number to to Date String</strong><br> | ||
<strong>Format: @18( <number> ) : STRING</strong><br> | <strong>Format: @18( <number> ) : STRING</strong><br> | ||
Returns date string "MM/DD/YYYY" from ADN in <number>. If ADN is invalid, returns "--/--/----". | Returns date string "MM/DD/YYYY" from ADN in <number>. If ADN is invalid, returns "--/--/----". | ||
|- | |- | ||
| @19 | | @19 | ||
| <strong> | | <strong>Date String to Absolute Day Number</strong><br> | ||
<strong>Format: @19( <string> ) : NUMERIC</strong><br> | <strong>Format: @19( <string> ) : NUMERIC</strong><br> | ||
Converts date string "MM/DD/YYYY" to ADN. Returns -1 if invalid. | Converts date string "MM/DD/YYYY" to ADN. Returns -1 if invalid. | ||
| Line 170: | Line 172: | ||
|- | |- | ||
| @21 | | @21 | ||
| <strong> | | <strong>Alphabetic Lowercase Filter</strong><br> | ||
<strong>Format: @21( <string> ) : STRING</strong><br> | <strong>Format: @21( <string> ) : STRING</strong><br> | ||
Returns <string> with all non-alphabetic characters removed and converts letters to lowercase.<br> | Returns <string> with all non-alphabetic characters removed and converts letters to lowercase.<br> | ||
| Line 176: | Line 178: | ||
|- | |- | ||
| @22 | | @22 | ||
| <strong>Compress Number for Disk</strong><br> | | <strong>Compress Number for Disk Storage</strong><br> | ||
<strong>Format: @22( <number> ) : STRING</strong><br> | <strong>Format: @22( <number> ) : STRING</strong><br> | ||
Returns a compressed numeric string for disk storage by packing two digits per byte (approximately half-length of STR$ output). Uses a special encoding to avoid producing a carriage return character.<br> | Returns a compressed numeric string for disk storage by packing two digits per byte (approximately half-length of STR$ output). Uses a special encoding to avoid producing a carriage return character.<br> | ||
| Line 182: | Line 184: | ||
|- | |- | ||
| @23 | | @23 | ||
| <strong> | | <strong>Expand Compressed Number</strong><br> | ||
<strong>Format: @23( <string> ) : NUMERIC</strong><br> | <strong>Format: @23( <string> ) : NUMERIC</strong><br> | ||
Inverse of @22: uncompresses a number stored in <string>. Invalid strings yield undefined results.<br> | Inverse of @22: uncompresses a number stored in <string>. Invalid strings yield undefined results.<br> | ||
| Line 188: | Line 190: | ||
|- | |- | ||
| @24 | | @24 | ||
| <strong> | | <strong>Format Relative-File Record</strong><br> | ||
<strong>Format: @24( <string> , <number> ) : STRING</strong><br> | <strong>Format: @24( <string> , <number> ) : STRING</strong><br> | ||
Pads/truncates a record to length <number> for relative file use.<br> | Pads/truncates a record to length <number> for relative file use.<br> | ||
| Line 197: | Line 199: | ||
|- | |- | ||
| @25 | | @25 | ||
| <strong> | | <strong>Reverse Substring Search</strong><br> | ||
<strong>Format: @25( <string1> , <string2> [ , <number> ] ) : NUMERIC</strong><br> | <strong>Format: @25( <string1> , <string2> [ , <number> ] ) : NUMERIC</strong><br> | ||
Same as @2, but searches from right to left (end of <string2> toward the beginning). | Same as @2, but searches from right to left (end of <string2> toward the beginning). | ||
|- | |- | ||
| @26 | | @26 | ||
| <strong> | | <strong>Extract Delimited Field</strong><br> | ||
<strong>Format: @26( <string1> , <string2> , <number> ) : STRING</strong><br> | <strong>Format: @26( <string1> , <string2> , <number> ) : STRING</strong><br> | ||
Returns the <number>th section of <string2> separated by the delimiter in <string1>.<br> | Returns the <number>th section of <string2> separated by the delimiter in <string1>.<br> | ||
| Line 209: | Line 211: | ||
|- | |- | ||
| @27 | | @27 | ||
| <strong>Calendar Age | | <strong>Calculate Calendar Age</strong><br> | ||
<strong>Format: @27( <number1> , <number2> ) : NUMERIC</strong><br> | <strong>Format: @27( <number1> , <number2> ) : NUMERIC</strong><br> | ||
Returns calendar age in years between two ADNs: from <number2> to <number1>.<br> | Returns calendar age in years between two ADNs: from <number2> to <number1>.<br> | ||
| Line 215: | Line 217: | ||
|- | |- | ||
| @28 | | @28 | ||
| <strong> | | <strong>Search One-Dimensional String Array</strong><br> | ||
<strong>Format: @28( <string> , <array> ) : NUMERIC</strong><br> | <strong>Format: @28( <string> , <array> ) : NUMERIC</strong><br> | ||
Searches a one-dimensional string array for an exact match to <string> and returns the index. Returns -1 if not found.<br> | Searches a one-dimensional string array for an exact match to <string> and returns the index. Returns -1 if not found.<br> | ||
| Line 222: | Line 224: | ||
|- | |- | ||
| @29 | | @29 | ||
| <strong>Read | | <strong>Read Fixed-Length Byte String</strong><br> | ||
<strong> | Format: <strong><nowiki>@29(<count>[,<skip>])</nowiki></strong> | ||
Reads and returns a string containing a fixed number of bytes from the logical file specified by !14. | |||
* <nowiki><count></nowiki> is the length of the returned string and must be from 1 to 255. | |||
* <nowiki><skip></nowiki> is optional. When specified, that number of bytes is read and discarded before the returned data is collected. | |||
* The returned string is always exactly <count> bytes long. | |||
* !40 is not changed by this function. | |||
* The file remains open after the function. | |||
* The current file status remains available through ST. | |||
The function does not use !04, !08, or !15, and does not stop at carriage returns or other delimiter characters. | |||
If the file ends or another file-status condition occurs before the requested number of bytes is available, the remainder of the returned string is filled with CHR$(0). Check ST when it is necessary to determine whether all bytes came from the file. | |||
Do not use a count of zero. | |||
Examples: | |||
* <strong>A$=@29(6)</strong> Returns the next six bytes from the file. | |||
* <strong>A$=@29(2,2)</strong> Discards two bytes and returns the following two bytes. | |||
|- | |- | ||
| @30 | | @30 | ||
| <strong>Boot Drive | | <strong>Build Boot-Drive Initialization Command</strong><br> | ||
<strong>Format: @30( <number> ) : STRING</strong><br> | <strong>Format: @30( <number> ) : STRING</strong><br> | ||
This function is used in conjunction with the variables !49, !50, and !51. | This function is used in conjunction with the variables !49, !50, and !51. | ||
| Line 234: | Line 252: | ||
|- | |- | ||
| @31 | | @31 | ||
| <strong> | | <strong>Enhanced Message-Record Read</strong><br> | ||
<strong>Format: @31 : (Undocumented)</strong><br> | <strong>Format: @31 : (Undocumented)</strong><br> | ||
Newer function (not documented in the v8.0 manuscript). See [[Undocumented_8.1_commands#at31|Undocumented features: @31]]. | Newer function (not documented in the v8.0 manuscript). See [[Undocumented_8.1_commands#at31|Undocumented features: @31]]. | ||
|- | |- | ||
| @32 | | @32 | ||
| <strong> | | <strong>Read Fixed-Length Numeric Value</strong><br> | ||
<strong> | Format: <strong>@32(<nowiki><count>[,<skip>])</nowiki></strong> | ||
Performs the same fixed-length file read as @29, then converts the returned byte string to a numeric value using the same internal conversion performed by @6. | |||
* It is functionally equivalent to <strong>@6(@29(<nowiki><count>[,<skip>]))</nowiki></strong> but performs the read and conversion as one function. | |||
* <nowiki><count></nowiki> is the number of bytes to read and must be from 1 to 255. | |||
* <nowiki><skip></nowiki> is optional. When specified, that number of bytes is discarded before the bytes used for the numeric result are read. | |||
* The logical file is selected by !14. | |||
* !40 is not changed. | |||
* The file remains open after the function. | |||
* The current file status remains available through ST. | |||
* Missing bytes following an end-of-file or other file-status condition are treated as zero bytes. | |||
Do not use a count of zero. | |||
Example: <strong>A=@32(2,2)</strong> | |||
This discards the first two bytes of a disk-directory record, reads the following two bytes, and returns their numeric value. This is commonly used to obtain the block count from a Commodore disk-directory entry. The same operation could be written as: <strong>A=@6(@29(2,2))</strong> | |||
|- | |- | ||
| @33 | | @33 | ||
| <strong>Return | | <strong>Return Saved Input Color</strong> | ||
This command accepts no arguments and returns a one-character string containing the PETSCII foreground-color control byte that was active when the most recent ML line-input operation began. It is used by the message editor to restore the input color after deleting temporary command prompts. Useful for character deletion, cursor movement, backward color-state reconstruction, prompt removal, and redisplay of edited text. | |||
|} | |} | ||
Latest revision as of 00:50, 31 July 2026
Programming Features - ML Functions
The ML functions provide operations that are more flexible than ML commands or ML variables. In practice, they behave like built-in BASIC functions: some execute an ML routine and then return a computed value. Functions can return either numeric or string results (including string values that cannot be returned through ML variables).
All ML functions begin with an "@" followed by one or two digits (the function number). Functions are either STRING or NUMERIC, as indicated in the table. Some functions accept required and/or optional parameters, which are placed in parentheses after the function number.
There are 22 string-returning functions & 12 numeric-returning functions.
Example:
1000 a$=@5:sr=st:a=val(a$):ifa<>0thenprint@1(a) 1010 ifsr=.then1000
In this example:
- @5 reads a line from disk (like .01) and returns it as a string.
- VAL() is used to test whether the returned line is numeric; if so, @1 prints the number without the leading space added by STR$ for positive values.
- The loop continues until end-of-file (as indicated by ST).
All ML functions (@##) invoke address $4E21 for ML processing.
The table below lists the ML Functions and descriptions.
| Function | Format and Description |
|---|---|
| @0 | Resident Input Buffer Format: @0 : STRING |
| @1 | Number to String Without Leading Space Format: @1( <number> ) : STRING |
| @2 | Forward Substring Search Format: @2( <string1> , <string2> [ , <number> ] ) : NUMERIC
Examples:
The !52 variable stores the most recent find position (0 if the count form was used). |
| @3 | Repeat Character Format: @3( <string> , <number> ) : STRING |
| @4 | Character Input Format: @4 : STRING |
| @5 | Read Disk Record Format: @5 : STRING |
| @6 | Byte or Word Extraction Format: @6( <string1> [ , <string2> ] ) : NUMERIC
Example: GET#8,A$:I=@6(A$)
Example: IF LEN(I$)=2 THEN L=@6(I$)
Example: GET#8,A$,B$:L=@6(A$,B$) |
| @7 | Word to Two-Byte String Format: @7( <number> ) : STRING |
| @8 | Interactive Line Input Format: @8 : STRING |
| @9 | Free Memory Without Garbage Collection Format: @9 : NUMERIC |
| @10 | Strip Control and Graphics Characters Format: @10( <string> [ , <number> ] ) : STRING |
| @11 | Current Time String Format: @11 : STRING |
| @12 | Overlay String at Position Format: @12( <string1> , <string2> , <number> ) : STRING
Special replace mode: if <number>=0, uses the most recent find position from @2 or @25. If that value is 0 (or if @2/@25 was used in count mode), an ILLEGAL QUANTITY error will occur. |
| @13 | Crash Error Message Format: @13 : STRING |
| @14 | Crash Error Line Number Format: @14 : STRING |
| @15 | Date to Absolute Day Number Format: @15( <year> , <month> , <day> ) : NUMERIC |
| @16 | Absolute Day Number to Year (Sets Month/Day) Format: @16( <number> ) : NUMERIC |
| @17 | Absolute Day Number to Day of Week Format: @17( <number> ) : NUMERIC |
| @18 | Absolute Day Number to to Date String Format: @18( <number> ) : STRING |
| @19 | Date String to Absolute Day Number Format: @19( <string> ) : NUMERIC |
| @20 | Current Overlay Filename Format: @20 : STRING |
| @21 | Alphabetic Lowercase Filter Format: @21( <string> ) : STRING |
| @22 | Compress Number for Disk Storage Format: @22( <number> ) : STRING |
| @23 | Expand Compressed Number Format: @23( <string> ) : NUMERIC |
| @24 | Format Relative-File Record Format: @24( <string> , <number> ) : STRING
Null strings or <number><2 may yield undefined results. |
| @25 | Reverse Substring Search Format: @25( <string1> , <string2> [ , <number> ] ) : NUMERIC |
| @26 | Extract Delimited Field Format: @26( <string1> , <string2> , <number> ) : STRING |
| @27 | Calculate Calendar Age Format: @27( <number1> , <number2> ) : NUMERIC |
| @28 | Search One-Dimensional String Array Format: @28( <string> , <array> ) : NUMERIC |
| @29 | Read Fixed-Length Byte String Format: @29(<count>[,<skip>]) Reads and returns a string containing a fixed number of bytes from the logical file specified by !14.
The function does not use !04, !08, or !15, and does not stop at carriage returns or other delimiter characters. If the file ends or another file-status condition occurs before the requested number of bytes is available, the remainder of the returned string is filled with CHR$(0). Check ST when it is necessary to determine whether all bytes came from the file. Do not use a count of zero. Examples:
|
| @30 | Build Boot-Drive Initialization Command Format: @30( <number> ) : STRING |
| @31 | Enhanced Message-Record Read Format: @31 : (Undocumented) |
| @32 | Read Fixed-Length Numeric Value Format: @32(<count>[,<skip>]) Performs the same fixed-length file read as @29, then converts the returned byte string to a numeric value using the same internal conversion performed by @6.
Do not use a count of zero. Example: A=@32(2,2) This discards the first two bytes of a disk-directory record, reads the following two bytes, and returns their numeric value. This is commonly used to obtain the block count from a Commodore disk-directory entry. The same operation could be written as: A=@6(@29(2,2)) |
| @33 | Return Saved Input Color
This command accepts no arguments and returns a one-character string containing the PETSCII foreground-color control byte that was active when the most recent ML line-input operation began. It is used by the message editor to restore the input color after deleting temporary command prompts. Useful for character deletion, cursor movement, backward color-state reconstruction, prompt removal, and redisplay of edited text. |
Next section: Basic Variables Table