There are certain programming tasks that sound simple but end up fascinating ... they sound straightforward (and probably are), but it turns out there are lots of ways of doing them and you could spend a lot of time working out which was is best. One of those is converting three letter abbreviations for months into numbers. Sounds easy, doesn't it ... well - it is, but which way would you go?
a) A series of "if" and "elseif" statements
b) A string of 36 letters JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC and see where your month name appears in that
c) A switch statement with 12 choices (Obviously not available on languages that don't have a switch)
d) An array (Tcl), Hash (Perl), Table (Lua), Associative array (PHP) or dictionary (Python) where the keys are the names of the month and the values are the month numbers
e) A list (Tcl, Perl), Tuple (Python) , indexed array(PHP) indexed table (Lua) through which you search for the month string and return the index you're on when you find it.
I've put up a sample - in Lua - showing (a), (b) and (d) - it's
[here] and comes from discussions on this week's course. There's no switch in Lua - and indeed the Lua user's Wiki sends you back to option (a) - see
[here]. And option (e) is so inefficient and horrid that I'm not going to lower myself to write that code in Lua (note, though that the
in operator in Python may make it acceptable there!
(written 2010-09-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U112 - Lua - Other Standard Library features. [1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18)
Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[642] How similar are two words - (2006-03-11)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2586] And and Or illustrated by locks - (2010-01-17)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2894] Sorting people by their names - (2010-07-29)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4652] Testing new algorithms in PHP - (2016-02-20)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4707] Some gems from an introduction to Python - (2016-10-29)
Some other Articles
Melksham Railway Station - new information pointRailway meetings, trips and meetups this autumnTurning an exercise into the real thing with extreme programmingConcluding the interview processLots of way of converting 3 letter month abbreviations to numbersCamilla, Duchess of Cornwall, turns first sod in Melksham Link CanalLua - variable function arguments and selectMelksham shamefully makes the national headlinesTeaching Lua to a Perl advocateLevel playing fields, job vacancies, lemons and peaches