[ABE.pm] Perl just blows me away...
Faber Fedor
faber at linuxnj.com
Fri Feb 6 15:47:13 CST 2004
Check this sh*t out
I keep a mileage log in my Palmpilot and it generates a tab-delimited
file. Since it's tax time, I have to found out how many
business/personal/commuting miles I drove in my two vehicles this year.
I just wrote the short script below tp parse the data file. I freaking
LOVE how Perl builds the data structure for me! Just four lines of code
(in the while loop) and the file is parsed and the mileage in each
category for each vehicle is automagically separated and tallied. Sweet!
<-----------------------------< cut here >-------------------------->
#!/usr/bin/perl -w
#-d:ptkdb
#
use strict;
die "Usage: ", $0 =~ /([^\/]+)$/, " <data_file> \n" unless @ARGV == 1;
my ($CARFILE) = @ARGV ;
my ($inf, @fields, %cars, $car, $cat) ;
open($inf, "< $CARFILE");
while( <$inf> ) {
next if m/^Car Start/ ;
chomp;
# $fields[0] = Car ("Car", "Rav4", etc.)
# $fields[3] = mileage
# $fields[13] = Category
@fields = split(/ /, $_);
$cars{$fields[0]}{$fields[13]} += $fields[3];
}
foreach $car ( sort keys %cars) {
print "Car $car\n";
foreach $cat (sort keys %{ $cars{$car} } ) {
print " $cat miles = $cars{$car}{$cat}\n";
}
}
--
Regards,
Faber
Linux New Jersey: Open Source Solutions for New Jersey
http://www.linuxnj.com
More information about the ABE-pm
mailing list