Really using print using
Home / Publications / Rainbow / Rainbow 1981 / Rainbow 1981-10Template:PageName PRINT USING is one of the better commands available to the programmer who is trying to format his output, to the screen or the printer. This command is available in Extended Color Basic.
Sometimes PRINT USING can be confusing! It’s really a fairly simple thing but, in response to a number of questions we have received in the past several weeks, here's a takeout on some of its more intricate little goodies.
(By the way, you won’t find a re-hash of anything that is in any o-f the COLOR COMPUTER manuals in the RAINBOW. Unlike some other publications, we believe you can read! When we do things of this sort, it will be in an effort to either clarify or to make you aware of things which may not have been fully covered in the "official" documentation.)
PRINT USING is what is sometimes called an "Image Statement" in that it creates an image for your output. The images are created by the pound-sign (#) for numbers and the space for strings. The most simple use of PRINT USING is for lining up the decimal points in number columns. While you can do this lining-up with code, it is cumbersome. (Note to non-Extended users: William Barden Jr.'s excellent "Programming Techniques for Level II Basic" explains how you can do this.)
But, back to using PRINT USING. You build your "image" in one of two ways. First you can define a string with a statement like AS="###.##" and stick it somewhere in your program before you actually use it. Then, every time you want a printout to come out with three digits to the left of the decimal and two to the right, you just write "PRINT USING AS;<variable>" where the variable is the number you want printed out in that form.
The second form is simply to define the image when you ask the variable to be printed out. In this case, you just write "PRINT USING "###.##";<variable> and it will be done. Note the semicolon is always required in all PRINT USING statements.
The disadvantage to form two is that you have to redefine the image each time you want a variable printed out. With. form one, you just use A$ (or whatever) in each instance you want the variable(s) printed out in a certain way.
All this is pretty routine, but you can do a few other interesting things with PRINT USING as well.
For one thing, you can make your output labels part of the image as PRINT USING " TOTAL COST ###.##";A. This will indent the words "TOTAL COST" two spaces, print them out, skip two more spaces and then print out the variable in the three places to the left/two to the right format. Of course, you can vary an.
Better yet, you can combine image statements on a single line. For instance, if you want a line which reads " TOTAL COST ###.## NET COST ###.##", all you have to do is write the image statement that way and then list the two variables after the image format, like this: PRINT USING " TOTAL COST ###.## NET COST ###.##";A,B
You can keep this up as long as you have space to print out your variables and images.
But there is more. You could, if you wished, define these same image strings at the start of the program (or anywhere before they are used, although the start is best) and then apply them when needed. To take our original example, we could define A$ as being equal to " TOTAL COST ###.##" and then simply use PRINT USING A$;A to get the same result.
Yes...there is still more! You can concatenate the image strings, thereby custom tailoring each to suit your needs on any particular line o-f output without the need to write complicated code -for each line. Spaces in between each label in a line could be previously defined as well.
As an example of all of this, let’s assume you define A$=" TOTAL COST ###.##", B$=" NET COST .##.##" and C$=" ". To print this out with variables, merely use: PRINT USING A$, C$, B$; A, B.
Or, if you want to concatenate, use: D$=A$+C$+B$:PRINT USINGD$; A, B. You don’t have to concatenate on the same line as you use PRINT USING. In fact, with a great deal of printout of this nature, you would probably want to set up your image segments first, then concatenate as necessary, and apply those concatenated strings as needed in your listing. It also makes your code look a little cleaner.
While this description has dealt mainly with numbers, you can apply the same principles to strings if you desire. Also, you should be aware of the other options available in PRINT USING (such as the dollar sign) which are explained in your manual.