#!/usr/bin/perl


# accounts.pl  Keep track of a small business's finances.
# 
#              See wikipedia:Double-entry Bookkeeping
#


# An array of ID numbers contains the price for
# each item in stock. Prices are in PENNIES
# so that they can be an integer number & avoid
# any wierdness.
#
# Another array of ID numbers contains the description
# for each of the items.

@prices = ();
@desc = ();

$prices[0] = 100;       $desc[0] = "Wrigley Chewing Gum";
$prices[1] = 150;       $desc[1] = "Manila Folders (3)";
$prices[2] = 125;       $desc[2] = "2 ft. USB cable";
$prices[3] = 900;       $desc[3] = "Home baked apple pie";


# Now, at the cash register, ask user for list of ID
# numbers. When a blank line is entered, print a 
# reciept with item names, prices, and a total cost.

@sales = ();                # Our list of what gets sold.
$done = 0;                  # Flag to see if done looping.

while (not $done) {
    print "Next item: ";
    $item = <STDIN>;        # This could come from laser scanner.
    if ($item eq "\n") { $done = 1; } 
    else {
        chomp $item;        # Get rid of newline character.
        push @sales, $item; # Append to list.
    }
}

# Go through the sales, print them and
# add up for a total. Note the conversion
# from an integer number of pennies to
# floating point style...
#
$i = 1;                     # May as well number them.
$total = 0;
print "\n";

for $id (@sales) { 
    $price = int($prices[$id]);
    $total += $price;
    $price = sprintf("%3.2f",$price/100);
    $descr = $desc[$id];
    print "$i. $descr\t\t$price \n"; 
    $i++;
}

print "-" x 36;
print "\n";
$total = int($total) / 100;
$total = sprintf("%3.2f",$total);
print "TOTAL\t\t\t\t$total\n";



syntax highlighted by Code2HTML, v. 0.9.1