A hash is a collection of scalars, but each element can be named rather than numbered as it would be in a list. This makes it similar to a database table in many ways and leads to some truly remarkable capabilities.
This topic is presented on public courses
Perl Programming,
Perl bootcamp
Examples from our training material
| account | Sample - extracting statistics from an access log file |
| acl | Sample answer - web_c2, page 152 |
| bankout | initialising a hash |
| cbdemo | How sort callbacks work |
| env2 | ENV module to import variables as globals |
| envrep | use of %ENV |
| h1 | Setting up, adding to, changing a hash (associative array) |
| hash1.pl | setting up and using a hash |
| nip2 | set up and loop though a hash in various ways |
| nip3 | the danger of jumping out of an each loop |
| nip4 | Sorting the keys of a hash |
| niports | hash - manipulation |
| pathit | List out all programs on the current $PATH |
| peeps.pl | Set up a hash of people |
| so2 | Sort using your own sort subroutine |
| so4 | sort using a more complex routine |
| so5 | sorting with a comparison block |
| sorter | Demonstration of default dort order |
| stdhash | keys must be unique - example illustrates the problem |
| stdhash3 | Hash keys must be unique - so make up a new keu |
| stdhash4 | Keys must be unique - design new keys, add accessor loops to find multiple keys |
| sthash2 | Keys must be unique - solution 1 - Append all possible values |
| ukports | fill and look up a hash |
| web_count | Sample answer - web access counter question 1 |
| webc2 | Sample answer - 2nd question |
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Setting up a hash.
Accessing a hash.
Individual elements.
The whole hash.
Processing every element of a hash.
Keys and values.
Each.
Ordering a hash (sorting).
Sorting using your own subroutine for comparison.
Operators cmp and <=>.
A more complex sort selector routine.
Sorting with a comparison block.
Programming techniques.
Non-unique keys.
Looking for matching keys.
Use hashes for stock numbers.
Deleting elements, clearing out hashes.
Initialising hashes.
Special hashes.
%ENV.
Summary.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 600 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.