|
Nothing beats Perl to solve a data manipulation requirement quickly
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)
5d43
Associated topics are indexed under P669 - Perl - Data Munging [3764] Shell, Awk, Perl of Python? - (2012-06-14) [3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25) [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
|
4088 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|