PRINT, LPRINT, DUMP (A New Adventure in Dataland)

by Welopez

Welcome to another Adventure in Dataland! On our last cruise aboard the Dataland Queen (bringing to mind visions of Bogart and Hepburn, and evil Germans), we sorted and edited a simple text file for "myTestData.txt." In this tutorial, just about everything we did before will be the same (hey, I'm not as spry as I once was, and it's been over 110° more than 10 consecutive days here!), but we'll add a few blocks of code to the basic program to demonstrate formatting output to the MAINWIN, your default printer, or a new text file.

Computers are marvelous machines. In business or industry, they can do billions of tasks which previously may have been performed by thousands of workers on weeks and weeks of company time. The task of the computer is normally to collect information, process it in some way (or many ways), and output the results in a manner suitable for human interaction. This usually means printing a hard-copy since computer technology has not yet progressed to the point where data can be directly connected to the CPU between our ears.

As a programmer, you will use PRINT and LPRINT statements many times in your hobby or career. We use PRINT statements to display text, colors, graphics, etc, on a GUI to display information on the computer monitor. Sometimes we PRINT to the MAINWIN, other times we send the information to be printed to the PRINTER or a file. Occasionally, no human ever sees the processed results of an application we've written, instead it is fed as DATA to another computer for other uses.

On our last safari, we printed everything to the MAINWIN from an array named myContacts$(). The modifications for this program are contained between remark lines:
' = = = = = Routines for the Print Demo go here.
Several blocks of code
' = = = = = End of Print Demo routines.

When you want to format output to the MAINWIN, or to your printer, you can modify the routines to achieve the formatting you desire. When printing to a file, .TXT, .DAT, .INI, or whatever name you've given it, data fields are separated using a delimiter. DATA files using a comma to separate data fields are called CSV (comma separated value) files. See the help topic, using the WORD$() function, to assign a delimiting value of your own.

Data printed to the MAINWIN can be aligned and formatted by concatenating strings to achieve the desired appearance, or by PRINT TAB(n); "data"; TAB(n2); "another value";... etc. In this demo, we've use the PRINT TAB(n) method. Because displays on your monitor will automatically scroll left and right, we don't need to be concerned with the length of the line to be printed.

When sending data to the printer, we must be conscious of the margins of the paper being used (normally 8½ by 11 inches). If you have many long lines of information to be printed, you may want to set your printer for "landscape" printing, or choose a smaller font for the printed output. Old timers like me generally do not appreciate text smaller than 12 points, but lawyers are extremely fond of 8 point (or smaller!) font sizes. Choose whatever is appropriate for your printed document.

In the last millennium, there was a time when a line printer meant exactly that, a printer to print one line at a time. Fan fold paper was common, and code had to be written to keep track of how many lines had been printed to a page and generate a page break to insure text would not be printed across the perforation separating pages.

Most home users now have sheet-feed printers, meaning text is printed to a page at a time before generating a page feed. Programmers generally do not have to keep track of how many lines are printed to a page because Windows and/or the printer drivers will generate a page feed when needed.

To send text to your printer, you code should include:


LPRINT "This is the first line of text."  'As an example, only.
LPRINT  'We didn't tell the code to print anything.  This prints only a line feed.
FOR i=1 TO 10
LPRINT "This is line "; (i+1);"."
NEXT i
LPRINT  'Another blank line.
LPRINT "End of print job."
DUMP

The code sends each line of text to the print buffer. Your program may have several pages for output, and you don't want to print each line to a separate page. The print buffer stores every line (now you know why printers specify 2MB, 4MB, 8MB of memory) and finally, when the DUMP command is issued, the printer completes the assigned job.

By default, printers generate a line feed with every LPRINT statement. You can suppress linefeeds by using the semicolon (;) in the LPRINT statement, as we did in the FOR/NEXT loop above. Suppressing a linefeed allows us to concatenate lines of text, to include variables from the program, if needed. Don't create lines which will exceed the margins of your paper... oh, well, go ahead and do that too. When you do it yourself, you'll learn how to format text for output.

Another method for printing output is to PRINT to a file. If no file exists, it will be created when you OPEN "myFile.txt" FOR OUTPUT AS #1 (using your own file and device name). If you OPEN "myFile.txt" FOR OUTPUT... the current contents of the entire file will be deleted and replaced with the information you are printing now. In the [showNew] block of this program, we OPENed "myContacts.txt" FOR OUTPUT and printed an entirely new file. In the [addNew] block, we OPENed "myTestData.txt" FOR APPEND and added but one line, because we did not want to lose all the information in our data file. In this program, "myTestData.txt" is always used to load information into the program, and "myContacts.txt" is always used to print OUTPUT by the program.

Any user can easily edit "myTestData.txt" using Notepad, but you'll usually want to write an add/edit routine to automate the task, because your end user may not be familiar with text files used as data for the program. In this demo, we opened "myTestData.txt" using Notepad, just to familiarize you with doing so. Each line of "myTestData.txt" holds four data fields. The structure of the data file must be maintained exactly, or the program will crash at runtime.

"WAIT A MINUTE, WELOPEZ! YOUR SORT ROUTINE BOMBED FOR ME! Alfred Youngfellow is the youngest person in the file, but when I sorted by age, he was at the very bottom of the list!"

"Tsk, tsk, tsk. You have not been keeping up with our previous Adventures in Dataland or you would know why. Basic will not allow us to store array values of mixed types. Either they are all numeric, or they are all strings. When we created the array for myContacts$(), we had to declare it as a string array because some elements would be names and others would be numbers, such as age or telephone."

BOOLEAN comparisons (equals, less than or greater than) of strings begins with the first character of every string. If the first character is equal, the next character is compared until an inequality is found, then the string is ordered according to ASCII value. The ASCII value of 3 is less than 6, for example, but the ASCII value of 47 (remember, first letter only, unless it is equal to the comparison, then try the next, and the next) is less than the ASCII value of 6. Always keep this in mind if you must have a precise sorting routine, and use the VAL() function to get the numerical value when appropriate.

If you want to have Youngfellow at the top of the list, you can pad his age with a leading zero, or as many as are needed. The ASCII value of zero is less than any of the numerical values, and he'll be at the top of the list.

"But when I have Youngfellow's age printed, I don't want it to say zero-six, that's silly!"

Okay then, use the PRINT USING("##", number) statement. If the leading character is zero, it will not be printed. Basic has provisions to do just about anything you need, you simply have to learn to speak Worble to get those little fellows to do what you want. You can print all the ages in this file USING("##", number), and they will be aligned with the right-most character of the string at the right of your column.

"Hmph! Okay, I can do that, but I want a pay-raise if I have to learn a second language!"

I'll be happy to double your salary as a coder, afterall, two times nothing is still nothing.

Don't spend it all in one place!