OK - I admit it. It might be slightly out of fashion but I still love Perl for that quick hack script which is a one-off data manipulation need.
I have two files - the first containing records of who signed our
pledge for an appropriate TransWilts train service last year, and another containing records in the same format for those who have signed
this year, and I wanted a list of people who haven't reconfirmed their support.
Here's a "break the rules of good programming" piece of code that I slapped together in a few minutes ...
open (FH,"08.cooked");
while (<FH>) {
chop;
$key = lc;
$signed{$key} = 1;
$lastyear{$key} = 1;
}
open (FH,"09.cooked");
while (<FH>) {
chop;
$key = lc;
$signed{$key} = 2;
}
foreach $previous (keys %signed) {
# last year only
if ($signed{$previous} == 1) {
print "$previous\n" ;
$awaited += 1;
}
if ($signed{$previous} == 2) {
# this year only
$newcomer += 1 if (! $lastyear{$previous}) ;
# this year and last year
$both += 1 if ($lastyear{$previous}) ;
}
}
print ("$awaited last year only\n");
print ("$both both years\n");
print ("$newcomer new this year\n");
... and after a couple of syntax errors and a couple of validation tests, it ran for me and fulfilled its job.
Coding rules broken.
1. Cut and paste the file read code - un-necessary duplication
2. Poorly (un-named!) variable - use of $_
3. Combined data and report output of STDOUT needed cutting about later
4. Lack of descriptive comment block
but job done, and I can move on with my once-only result set and take a look through the output, considering and acting on the data provided which was the purpose in the first place.
P.S. Had I been working from database tables, a left join looking for orphan records would have let me do the whole data thing as a one-liner:
mysql> select pledges.email from pledges left join pledge09 on pledges.email = pledge09.email where pledge09.pid is NULL;
with slight modifications for each of the counts |
We offer courses in both
Perl Programming and in
MySQL. One of the most popular pages on our web site explains
the difference between the types of join in MySQL and how to extract all the different data combinations you might want.
(written 2009-04-14, updated 2009-04-16)
Associated topics are indexed under
P669 - Perl - Data Munging [3335] Practical Extraction and Reporting - (2011-06-26)
[2702] First and last match with Regular Expressions - (2010-04-02)
[1947] Perl substitute - the e modifier - (2008-12-16)
[1509] Extracting information from a file of records - (2008-01-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[597] Storing a regular expression in a perl variable - (2006-02-09)
P772 - Practical Example - Perl in use [2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
[2047] Small Web Server in Perl - (2009-02-18)
Some other Articles
Bigger and better this year!Learning about LurchersPatterns in street namesJavascript - move cursor over image to change a different image.Nothing beats Perl to solve a data manipulation requirement quicklyWiltshire - where everybody mattersCall in the professionals!Weeding out old phone numbersWe have lost a regular business guestBuilding down expectations