Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Looking up a value by key - associative arrays / Hashes / Dictionaries

Fallow DeerIn any substantial programming task, you'll want to store a whole series of values in some sort of table, so that you can process / reprocess / handle each of the elements in turn - and to do that, you'll use a variable construct that's called an "array", or a "list", a "tuple" or a "vector". (The exact word depends on the particular language you're using ... and how it's been implemented internally).

Arrays, lists, tuples and vectors are what we call "ordered collections" - in other words, they're bundles of variables which are keyed / looked up by position numbers. The numbering usually starts at 0 (exceptions - Lua and Fortran which start at 1), and in many cases that's ideal - but not in all cases.

Imagine that I have a data set which shows the UK ports from which you can take a ferry over the English Channel, and their destinations - [here]. The sort of question I'll be asking is "where can I go from Portsmouth" and if I keep the data in an array or list - keyed by position number - I'll have to do a search of some sort every time I make an inquiry; chances are that I'll need two arrays which have to be kept in step. Ironically, my user really won't care if Portsmouth is at position 0, 1, 2, or 3 ... nor if Newhaven comes first ... There has to be a better way!

There is. It's called an "Associative Array" (PHP), a "Dictionary" (Python), a "Hash" (Perl), (a table in Lua, a Hash, Hashmap or hashset in Java ...). In dictionaries and hashes, the elements are not orderable but can be looked up by key. There's a source code example (in Perl) [here] showing the setup and use of a hash for looking up our channel ports from our channel file - and we cover this in detail on our Learning to program in Perl course.

In PHP, things are a bit different. The Associative array CAN be ordered (sorted) - which overcomes one of the limitations of hashes and dictionaries, at the expense of efficiency on enormous data tables. But since PHP is going to be used primarily for web page generation, where the time taken / data sent can't be huge, this loss of efficiency in exceptional cases is no big deal. What does this mean?

It means that in PHP, I can set up a table of data - key, value pairs. I can then sort that table, and output the results in order. And I can do it VERY EASILY! I wrote an example of this - using fictional data for the number of deer in various national parks in the UK - on yesterday's PHP course, and I've shared the source code [here]. The code is robust (it has to be if it's going to be run on a public web site!) and you can run it [here].

(written 2010-08-11, updated 2010-08-20)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H106 - PHP - Arrays
  [4244] Disambiguation - PHP List - (2014-03-07)
  [4072] Splitting the difference with PHP - (2013-04-27)
  [4068] Arrays in PHP - contain different and even mixed data types - (2013-04-24)
  [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
  [3379] Sorting data the way YOU want it sorted - (2011-08-05)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2274] PHP preg functions - examples and comparision - (2009-07-08)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [1614] When an array is not an array - (2008-04-17)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1116] PHP adding arrays / summing arrays - (2007-03-23)
  [832] Displaying data at 5 items per line on a web page - (2006-08-14)
  [773] Breaking bread - (2006-06-22)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [409] Functions and commands with dangerous names - (2005-08-11)

P211 - Perl - Hashes
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
  [3106] Buckets - (2010-12-26)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [1917] Out of memory during array extend - Perl - (2008-12-02)
  [1856] A few of my favourite things - (2008-10-26)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [968] Perl - a list or a hash? - (2006-12-06)
  [930] -> , >= and => in Perl - (2006-11-18)
  [738] (Perl) Callbacks - what are they? - (2006-05-30)
  [386] What is a callback? - (2005-07-22)
  [240] Conventional restraints removed - (2005-03-09)

Q101 - Object Orientation and General technical topics - Programming Principles
  [4645] What are callbacks? Why use them? An example in Python - (2016-02-11)
  [4632] Remember to ask the question before you listen for the answer - (2016-01-26)
  [4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [4206] Writing the perfect program in Tcl? - (2013-11-13)
  [4153] Rooms available tonight - how to code an algorithm from first principles - (2013-08-19)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4090] Test Driven Development in Python - Customer Comes First - (2013-05-16)
  [4061] Seamless, integrated IT - we have a long way to go! - (2013-04-11)
  [4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
  [3954] Lesson 1 in programing - write clean, reuseable and maintainable tidy code - (2012-12-16)
  [3928] Storing your intermediate data - what format should you you choose? - (2012-11-20)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
  [3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
  [2769] Easy - but for whom? - (2010-05-18)
  [2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2510] The music of the stock market - (2009-11-22)
  [2415] Variable names like i and j - why? - (2009-09-22)
  [2327] Planning! - (2009-08-08)
  [2310] Learning to write high quality code in Lua - (2009-07-30)
  [2228] Where do I start when writing a program? - (2009-06-11)
  [2022] Pre and post increment - the ++ operator - (2009-02-03)
  [2001] I have not programmed before, and need to learn - (2009-01-19)

Y107 - Python - Dictionaries
  [4668] Sorting a dict in Python - (2016-04-01)
  [4661] Unique word locator - Python dict example - (2016-03-06)
  [4469] Sorting in Python 3 - and how it differs from Python 2 sorting - (2015-04-20)
  [4409] Setting up and using a dict in Python - simple first example - (2015-01-30)
  [4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [3934] Multiple identical keys in a Python dict - yes, you can! - (2012-11-24)
  [3555] Football league tables - under old and new point system. Python program. - (2011-12-18)
  [3554] Learning more about our web site - and learning how to learn about yours - (2011-12-17)
  [3488] Python sets and frozensets - what are they? - (2011-10-20)
  [3464] Passing optional and named parameters to python methods - (2011-10-04)
  [2994] Python - some common questions answered in code examples - (2010-10-10)
  [2986] Python dictionaries - reaching to new uses - (2010-10-05)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [1145] Using a list of keys and a list of values to make a dictionary in Python - zip - (2007-04-13)
  [1144] Python dictionary for quick look ups - (2007-04-12)
  [955] Python collections - mutable and imutable - (2006-11-29)
  [103] Can't resist writing about Python - (2004-10-29)


Back to
Older picture.
Previous and next
or
Horse's mouth home
Forward to
Testing the robustness of our hotel and training systems - holiday and sickness times
Some other Articles
London to Calne, Corsham, Melksham, Bradford-on-Avon, Chippenham by public Transport
Downloading a report from the web for further local analysis
Upload Image, Store in database, redisplay in browser. PHP and MySQL
Testing the robustness of our hotel and training systems - holiday and sickness times
Looking up a value by key - associative arrays / Hashes / Dictionaries
Older picture.
Six languages in one file - an HTML++ web page
Predictions for the seagull population
Feeding the Grockles
Robust - testing the system
4759 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, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2915_Loo ... aries.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat