Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Perl module P211
Hashes
Exercises, examples and other material relating to training module P211. This topic is presented on public courses Perl Programming, Perl bootcamp, Learning to program in Perl, Perl Programming

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.


Articles and tips on this subjectupdated
3662Finding all the unique lines in a file, using Python or Perl
A question - how do I process all the unique lines from a file in Python? Asked by a delegate today, solved neatly and easily using a generator which means that there's no need to store all the data - unique values can be passed back and processed onwards as they're found. This is fantastic news if ...
2012-03-24
 
3451Why would you want to use a Perl hash?
"What would I use one of THOSE for?" - a question often asked by newcomers to Perl when I first introduce hashes. Well - in writing a script to count up how many articles I've written about each subject we teach on, I came up with a perfect example. A hash is described as an "unordered collection" ...
2011-09-20
 
3400$ is atomic and % and @ are molecular - Perl
"When do I use a $, when do I use an @, and when do I use a % ?" That is a frequently asked question on a Perl course, where a delegate had dabbled with a bit of Perl ahead of time. You use @ if you're referring to the whole of a list - in other words a complete collection. If you're a chemist, you ...
2011-08-20
 
3106Buckets
"What is a bucket?" ... A question asked during a recent programming course, and probably not one to answer along the lines of "an open topped plastic or metal receptacle to catch, hold or transport water, sand or other materials." In programming terms, a bucket is a software unit of memory allocation, ...
2011-01-03
 
3072Finding elements common to many lists / arrays
"What do these lists have in common". That's a common question - but what does the person asking it actually mean? Is (s)he looking for items that occur in all of the lists, but perhaps at different positions? For items that occur in two or more of the lists? For items that occur at the same position ...
2010-11-27
 
3042Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl
Imagine you have a tree - a series of leaves which join together as you head towards the root into bigger and bigger branches. The "Least Common Ancestor" is the point at which the branches from two leaves you have chosen come together. In computing, such a tree structure is very common - files and ...
2010-11-11
 
2915Looking up a value by key - associative arrays / Hashes / Dictionaries
In 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 ...
2010-08-20
 
2920Sorting - naturally, or into a different order
There's a natural sort order for many things - for numbers, it's ascending, for words it's a dictionary order, for names it's by surname. But sometimes you want to sort a list of things different, or there's no provided way within a programming language to apply the natural sort to your type of thing. In ...
2010-08-20
 
2836Perl - the duplicate key problem explained, and solutions offered
Do you want to hold data in a hash in Perl - but you can't do so directly because you have more than one piece of data that should have the same key? The problem ... while (<DIAL>) {   ($dial,$place) = split   $codes{$place} = $dial;   } And each line that contains ...
2010-06-28
 
2833Fresh Perl Teaching Examples - part 2 of 3
Three part article ... this is part 2. Jump to part [1] or [3] Perl is the most tremendously powerful and fully featured langauge - you've probably seen that from the first set of sample programs from last week's course, which I wrote up over the following weekend to share with a wider audience. Here ...
2010-06-27
(longer)
1917Out of memory during array extend - Perl
Use a hash if you want a sparse list in Perl! If you set up a list in Perl and don't fill all the members from zero up, that's OK but all the missing elements will actually use up some memory as they'll point to an "undef" value. So this happens: Dorothy:ppcsrd08 grahamellis$ perl $num[1223776336] ...
2008-12-10
 
1856A few of my favourite things
"Raindrops on roses and whiskers on kittens; Bright copper kettles and warm woollen mittens ..." Sorry - the raindrops get me all wet, and I always get caught up on the thorns of roses. Mittens restrict the hands and make me even more butter-fingered than I am normally ... isn't it great that we're ...
2008-11-02
 
1826Perl - Subs, Chop v Chomp, => v ,
During courses, I end up writing a lot of short demonstrations to show particular features of a language - this week, it's a Perl Programming Course so those examples are in Perl. Some interesting Perl facts ... a) The => operator can be used to replace the , ("the equals, greater than can be used ...
2008-10-08
 
1705Environment variables in Perl / use Env
If I say use Env;, Perl loads in a module which allows me to access all my environment variables directly within my code, by name. If I add an explicit list of variable names as a list to the module, then only those variables will be imported. So in this example, $PATH prints nothing but $SHELL and ...
2008-07-12
 
1334Stable sorting - Tcl, Perl and others
Have you come across a STABLE sort? A Stable sort is one in which all the incoming elements which evaluate to an equal value when tested for sorting purposes remain in the same order in the output as they were in the input. Perhaps I had better give you an example. I have a log file and there's a ...
2007-09-07
 
968Perl - a list or a hash?
You can hold multiple scalars in either a list or a hash in Perl. A list (signaled by an @ character or [..] around the subscript) is ordered - i.e. the elements are numbered and the order is significant. In contrast, a hash (signalled by a % character or {..} around the subscript) is unordered; the ...
2006-12-07
 
930-> , >= and => in Perl
Yes, Perl is eclectic - lots of things it can do, taken from lots of places and so, yes, it does have all three of those operators! -> Run the method named to the right on the object named on the left >= Return 1 (a true value) if the expression to the left is numerically greater than the expression ...
2006-11-18
 
240Conventional restraints removed
IT without boundaries So say the coffee mugs where I'm training this week - a company motto that seems very appropriate when I'm teaching about Perl's lists. Most languages allow you to define a whole series of values in a single variable (an array), but if you're writing in C or Java, you're probably ...
2006-06-05
 
738(Perl) Callbacks - what are they?
In most programming applications, you'll write code that calls system functions or subroutines - for example, you'll write a program that reads data from a file (via a system call), splits the data into an array (perhaps via a further system call), and prints it out (through system calls). On a few ...
2006-06-05
 
Examples from our training material
acc_0   Loading from a module in another file
access_log.xyz   Log file used in many Well House exercises
account   Sample - extracting statistics from an access log file
acl   Sample answer - web_c2, page 152
aco_1   Access log - listing hosts via a hash
bankout   initialising a hash
blx.pl   Use of hashes in Perl / CGI
cbdemo   How sort callbacks work
channel   Ferries to France - data file
env2   ENV module to import variables as globals
envrep   use of %ENV
ford.pm   Module for use with account demo
fwom   Hash - keys must be unique
h1   Setting up, adding to, changing a hash (associative array)
ha1   Setting up, then displaying contents, of a hash
hash1.pl   setting up and using a hash
im3   Using intelligent matches to check hash keys
langies   List of all the skills in a file, and who has them
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
requests.xyz   Data file for hash demos and exercises
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
stdcodes   Dialling codes for use with course exercises
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
Pictures
Everyone's concentrating on the exercise
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 [here].
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 Ruby, Lua, 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 700 books on the subjects on which we teach. These books are available for reference at our training centre.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/P211.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb