Formatting the Receipt Attractively (CLI operation)
Coping with Variable Width Data
Taking this assignment as an example, one of the problems with output is making it seem appropraite and attractive.
In this case we'd like to present a receipt on screen. Ideally a recipt array might also be used for other purposes but for now, formatting is a big enough job.
The crux of the problem is that we would like two neat columns side by side. The first with the product descriptions, the second with the prices. This becomes a problem because we can't know in advance the length of the product description, or necessarily the number of characters in the price.
In Word Processing you could define a left tab for the left of the recipt, and a decimal tab for the numbers. Not so in this case, where all we appear to have is the slightly unpredictable \t command, which you should try first, as it may work for you.
The problem breaks down to this:
- Whatever the length of the description, we want to pad it with spaces on the right hand side to make each description the same effective width.
- Whatever the number of characters, we want to pad the price with spaces on the left hand side to make the prices line up correctly.
- Step 2 above assumes that each price has the same number of places after the decimal point in order to gain a neat columnar effect.
How long is your piece of string?
If the above steps are followed we should have a perfectly standard receipt format; the only tricks are knowing the numbers of characters in use and then knowing how to pad as required!
Take the following as an example. If three items are placed on the receipt using no formatting they'll probably look similar to this:
DESCRIPTION PRICE
DVD-R 5's 6.99
650W Power Supply 45.5
Vanilla Candles 2.25
Clearly, this displays the relevant data and is functional in that respect, but there are few marks to be had for screen presentation from this!
The first trick is to decide the Desired_Width of the column for the descriptions. Then determine the length of the description in any given line. Work out many characters are short from the width. Then print that number of spaces. That then moves the prices column an even distance across the screen, allowing you to format that properly later.
You could print this as a formula like this....
The key thing is to determine the Description_Length for each line. Thankfully all programming languages provide a tool to get the length (in characters) of a string. To confuse the enemy it's called the length function. You can see it in use in this program sample (which you may try under the name length01.pl if the operation of the function isn't immediately clear).

In Spaces nobody can hear you scream
OK. So thats the easy bit: working out the spaces required. You now know the Spaces_To_Print value from the earlier formula. Off you go, that's easy; just a little thought required to print the correct number of, well, spaces....
One way to do this is as something like follows:
# Based on the formula above...# and assuming $description[counter] is the description matching
# a barcode you've just read. Plinka plonka ploody pou.
# PS: You wouldn't normally use CAPITAL letters in a variable name.
$Spaces = $Col_Width - length $description[counter];
$sp_count = 1;
while ($sp_count < $Spaces)
{
print " "; # print just one space (at a time)
$sp_count++;
}
Follow the money...
Use the information on number formatting to set up the neat column of numbers. But instead of something like %3.2f as formatting information change the 3 to a trial value of, say, 7 and all your numeric output will occupy a tidy 7 spaces.If all goes well, expect output like this:
DESCRIPTION PRICE
DVD-R 5's 6.99
650W Power Supply 45.50
Vanilla Candles 2.25