| Home CBM ASCII-X BASIC Disk Commands Enter RUN mode Program Format Secret Variables Variable Format Expressions Keywords (divide) (equal) (less) (minus) (more) (multiply) (plus) (power) Abs And Append Asc Atn Auto Backup Bank Begin Bend Bload Boot Box Bsave Bump Catalog Char Chr Circle Close Clr Cmd Collect Collision Color Concat Cont Copy Cos Data Dclear Dclose Dec Def Delete Dim Directory Dispose Dload Do Dopen Draw Ds Ds string Dsave Dverify El Else End Envelope Er Err Exit Exp Fast Fetch Filter Fn For Fre Get Get num Getkey Go Gosub Goto Graphic Gshape Header Help Hex If Input Input num Instr Int Joy Key Left Len Let List Load Locate Log Loop Mid Monitor Movspr New Next Not Off On Open Or Paint Peek Pen Pi Play Pointer Poke Pos Pot Print num Pudef Quit Rclr Rdot Read Record Rem Rename Renumber Restore Resume Return Rgr Right Rlum Rnd Rreg Rspcolor Rsppos Rsprite Run Rwindow Save Scale Scnclr Scratch Sgn Sin Sleep Slow Sound Spc Sprcolor Sprdef Sprite Sprsav Sqr Sshape St Stash Step Stop Str Swap Sys Tab Tan Tempo Then Ti Ti string To Trap Troff Tron Until Using Usr Val Verify Vol Wait While Width Window Xor Syntax Tokens C128 D64plus Disk Escape Codes Hardware PCxface PETSCII Pet2asc Futurama IBM PC-AT Contact Games Glossary Hall of fame Hall of shame Miscellaneous Privacy policy Programming Twisty puzzles |
CMD fileNumber [ , USING format ; expression [ , expression ] ... ] [ { , | ; } ] ~ or ~ PRINT [ USING format ; expression [ , expression ] ... ] [ { , | ; } ] ~ or ~ PRINT# fileNumber [ , USING format ; expression [ , expression ] ... ] [ { , | ; } ]
Print an expression(s) with a specific format(s).
The USING preposition makes a fundamental change to PRINT(#)/CMD: it makes a list of expression(s) required and forbids the use of SPC and TAB. It allows a custom format for each expression. This is the BASIC equivalant of C's "printf" family of functions (altough it is less versitilie). The USING preposition is pretty versatile; it will accept any number or type of expressions that conform to the format. The format consists of one or more fields. The main requirement (well, recommendation) is the type of each expression corresponds with the type of field in the format string. Each field is specified with one or more special characters:
There are four possible "logical characters" that may be generated by the USING preposition (these are secret variables that can be set with PUDEF):
If an expression results in more characters than reserved for a field in the format, then BASIC will do one of two things, depending on the type of expression. For a string expression, the first (left-most) characters of the expression will be output (the string is right-truncated). For a numeric expression, it will generate a * for all characters that would normally hold a digit. If an expression uses fewer chacters than allocated by its field, the unused will be filled with blank(s). For string expressions, the BASIC action of right-trunctation of "too many characters" seems fine for the default (left-aligned) field format. It is questionable in regards to centered string expressions, and (in my opinion) is totally inappropriate for a right-aligned string. Unlike the + and - Field Chars (which are mutually exclusive), a field may include both = and > characters (to center or right-align a string, respectively); in this case, the last such character in the field determines the output format. For a numeric expression, the field is output (conceptually) as right-aligned. This is opposite of normal PRINT(#)/CMD behavior. Actually, numeric expressions are aligned with the decimal point, but this only appears in the output if it is included in the field of format. Unfortunately, there is no way to left-align or center a numeric expression. Possible work-arounds are to use PRINT(#)/CMD without USING to get left-aligned numbers, but then you can't control the format. Also mixing BASIC statements which alternate between the inclusion and exclusion of USING can be a bit confusing (it also tends to make the program longer). This only works for left-alignment or right-alignment. Another work-around (also not very acceptable) is to output the number as string expression (i.e., use STR$ on the numeric expression to make it a string). This method works for any string alignment: (default) left-alignment, center, or right-alignment. Using a string field has the problem of loosing control of the numeric format; however you do not need to alternate between inclusion and exclusion of the USING preposition. See examples. Unfortunately, there is no direct way to force leading zeros on a numeric expression. One kludge to accomplish this is to redefine the blank to be a zero (instead of the default space). However this will require using PUDEF; often it requires multiple uses of PUDEF since you usually don't want all numbers to have leading zeros. Further, this work-around fails when used in combination with a floating monetary symbol or seperator(s). In the case of PRINT USING, output is sent to the (active) display screen; for CMD/PRINT# USING the output is sent to the device which corresponds with fileNumber (see OPEN) which may (but usually doesn't) refer to the display screen. If the output does go the screen, many secret variables may be modified by printing control codes (see the PETSCII table). If fileNumber was not previously OPEN'd, then a FILE NOT OPEN ERROR occurs. If a field includes both + and - Field Chars or more than 1 decimal point then SYNTAX ERROR will be generated. Failure to include at least one # in format (or completely omitting format) generates SYNTAX ERROR. Failure to supply any expressions at all causes SYNTAX ERROR as well. Attempts to include TAB or SPC prepositions/functions will also generate SYNTAX ERROR. Unlike most BASIC commands and statments, no TYPE MISMATCH ERROR will occur if the expression does not match its corresponding field type in the format; in such cases, the type of expression "wins" the debate. BASIC will use all Field Chars (regardless of format type) to output the expression; in such a case, all formatting is "lost", every field character will be interpretted as a generic # character. This does not mean TYPE MISMATCH ERROR is impossible however; it may occur if an expression attempts to combine string and numeric values (without an appropriate conversion function). There are other issues with USING. One issue is that although a "floating" monetary symbol is possible, a "floating" sign is not (unless you want the default "negative only" behavior). A related issue is that the monetary symbol can only be fixed at the beginning of the field. Another issue is that if a decimal point and trailing #(s) are not present, BASIC will round a numeric expression to generate an integer output (by itself, not suprising) -- However, when USING rounds a number (this could be round to integer or round to a fraction) it uses the "round to nearest" forumula, unlike BASIC's more typical "floor" method (see INT for the case of integers / no fraction). If the format contains more fields than there are expressions, output will end (without error) at the the first field which is missing a corresponding expression; no part of the missing missing field will be output. If there are more expressions than fields in the format, then the format is re-used from the beginning (with no spaces or carriage returns between each format that is output). See examples. The optional terminating semicolon (;) will cause the next output to "run together" with this statement's output. In other words, no "new line" (see below) is sent to the output device. The optional terminating comma (,) will (SCREEN DISPLAY ONLY) align the next output on a 10-column boundry (0-based). It will also supress a "new line" (see below). See PRINT for examples of the comma. For non-screen devices, it functions the same as a terminating semicolon (because BASIC has no idea of the device's "output position"). If the statement does not end with one of the two optional punctuation marks (a comma or semicolon), then PRINT(#)/CMD will end by sending a "new line" to the output device. By default, this is a terminating carriage return (code 13). Under some cases (see OPEN) it may also send a terminating line feed (code 10). Examples of not-a-Field-Char:
Examples of too many or too few characters:
Examples of mutually exclusive field options:
Examples of numeric alignment (with numeric-only Field Char present):
Examples of numeric alignment (no numeric-only Field Char present):
Examples of monetary symbol and separator:
Examples of sign and more-expressions-than-fields (1 field, but 3 expressions):
Example of more-fields-than-expressions (3 fields, but 1 expression):
Examples of "too small" number and "scientific" format:
© H2Obsession, 2014 |