#!/usr/bin/perl use strict; use warnings; my $head = <<'EOT'; Gas Tank History

Gas Tank History

This table omits the first 3 tanks.

EOT my $fmt = <<'EOT'; EOT my $sum_fmt = <<'EOT'; EOT my $tail = <
# When Reciept MFD Computed Notes
Date Odo Trip Gal $/Gal $/Tank MPG (Gal) (Diff) MPG ¢/Mile
Current Data
%d %s %d %.1f %.3f %.3f %.2f %.1f %.3f %.3f %.1f %.3f %s
%.2d %.3f %.3f %.2f %.1f %.3f %.3f %.1f %.3f Summary of Current

Tkil
Last modified: Fri Oct 14 00:50:30 MDT 2005 EOT my $tank_nr = 0; my $cum_mi = 0; my $cum_gal = 0; my $cum_cost = 0; my $cum_mfd_mi = 0; my $cum_mfd_gal = 0; my @class = ( 'even-row', 'odd-row' ); my $table = ''; open my $fh, "gas-history.txt" or die "$0: opening 'gas-history.txt': $!"; while ( my $line = <$fh> ) { next if $line =~ /^#/; next unless $line =~ /\S/; my ( $ts, $odo, $trip, $gal, $usd_gal, $tot_usd, $mfd_mpg, $mfd_mi, $temp, $loc, $elev, $notes ) = split /\t/, $line; ++$tank_nr; $cum_mi += $trip; $cum_gal += $gal; $cum_cost += $tot_usd; my $mfd_gal = 1.0 * $mfd_mi / $mfd_mpg; $cum_mfd_mi += $mfd_mi; $cum_mfd_gal += $mfd_gal; my $mfd_gal_diff = $mfd_gal - $gal; $ts =~ s/ / /g; $notes = '' unless defined $notes; for ( $notes ) { s/&/&/; s//>/; s/"/"/; s/'/'/; } my $class = $class[ $tank_nr & 1 ]; my $mpg = $trip / $gal; my $cpm = 100.0 * $tot_usd / $trip; $table .= sprintf $fmt, ( $class, $tank_nr, $ts, $odo, $trip, $gal, $usd_gal, $tot_usd, $mfd_mpg, $mfd_gal, $mfd_gal_diff, $mpg, $cpm, $notes ); } my $mpg = $cum_mi / $cum_gal; my $cpm = 100.0 * $cum_cost / $cum_mi; my $avg_usd_gal = $cum_cost / $cum_gal; my $avg_mfd_mpg = $cum_mfd_mi / $cum_mfd_gal; my $sum = sprintf $sum_fmt, ( $cum_mi, $cum_gal, $avg_usd_gal, $cum_cost, $avg_mfd_mpg, $cum_mfd_gal, $cum_mfd_gal-$cum_gal, $mpg, $cpm ); print $head, $table, $sum, $tail; exit 0;