Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Perl, PHP or Python? No - Perl AND PHP AND Python!
"Which language is best for xxx task" I am often asked. And I'll listen to the questioner's description of his application, his background, and the sort of support he'll have at his place of coding before coming up with a suggestion. Never would I suggest he learn all three.

And yet, today, I found myself with a little task to do - it took me no more that a few minutes - and I used all three languages without even thinking about it.

The task. A 900 Mbyte web log file - recording all the traffic from the last year at our First Great Western Customer Forum and a requirement to get some information out of it about the geographic spread of the visitors.

1. A little bit of PERL (the Practical Extraction and Reporting Language) to pull all the different IP addresses that have visited the site out of the file.

#!/usr/bin/perl -na
$counter{$F[0]}++;
END {
@hl = sort keys %counter;
print $#hl+1," unique hosts\n";
foreach $ak(@hl) {
  print "$ak $counter{$ak}\n";
}}



2. A PHP script - as we have the PHP / MaxMind routines easily accessible - to add the country of origin for each visitor to the data (that's right - PHP running from the command line!)

<?php
include("../../include/stdphp.v8");
print "never mind!\n";
foreach (file("uniquehosts") as $line) {
  $lns = explode(" ",$line);
  $coco = getcountry($lns[0]);
  if (ereg("...",$coco[id])) {
  print "$coco[id] $line"; } else {
  print "XXX $line"; }
}
?>


3. Another piece of Perl to count the number of visiting IP addresses from each country

while (<DATA>) {
  ($c,$ip,$k) = split;
  $byc{$c}++;
  $byk{$c}+= $k;
}
foreach $co(sort {$byc{$b} <=> $byc{$a}} keys %byc) {
  print "$byc{$co} $co\n";
}

__END__
XXX 116.122.81.28 32
XXX 116.199.208.54 7
XXX 116.214.24.78 34
etc


4. and finally Interactive Python in place of a calculator to do some final statistical arithmetic on the results.

OK - I'll admit it; I got caught miscoding once by the different format of foreach in Perl and PHP, but otherwise it all worked pretty well. If you want to see the information extracted by this process, look here. And, no, I'm not really proud of the beauty of my code; written to fulfill a quick one-off hacking job.

If you think I've not given Python a fair run above, you're right. But then I have been using it all day as a notepad as I do my final personal tax calculations, with all the additions keyed in so I can go back and check my sums as I may need to over the next few days. That's code and numbers that I won't be sharing here, of course!
(written 2008-01-20 21:18:56)

 
Associated topics are indexed under
Q777 - Object Orientation and General technical topics - Technology Overviews
Q904 - Object Orientation and General technical topics - Analysing a Programming Task
G902 - Well House Consultants - Web site techniques, utility and visibility

Back to
Summer Ball at Bowood - Saturday 12th July 2008
Previous and next
or
Horse's mouth home
Forward to
Autovivification - the magic appearance of variables in Perl

Some other Articles
Python - formatting objects
Pictures you can use - for free - from our library
Keeping staff up to date on hotel room status
Autovivification - the magic appearance of variables in Perl
Perl, PHP or Python? No - Perl AND PHP AND Python!
Summer Ball at Bowood - Saturday 12th July 2008
Buses from Well House Manor, Melksham, to Bath
Handling Binary data (.gif file example) in Perl
Extracting information from a file of records
How not to write Perl?
1707 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 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).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho