Training, Open Source Programming Languages

This is page ... ogram.html

Our email: • Phone: 01144 1225 708225

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))
Setting up a matrix of data (2D array) for processing in your program

When you're reading and processing data, it often comes in the form of a series of records, with each record being split into a series of fields, and you'll often want to be going through the data several times, looking at different rows and colums, sorting them, comparing them, and so on. If the amount of data isn't so huge that you can't hold it all in memory at once, the best solution of often to read all the data at the start and store it into a collection variable, with each member of the collection being a collection itself. If this sounds very theoretic, it's what is colloquially known as a "2 dimensional array" or sometimes as a table.

Many modern languages don't have an explicit 2 dimensonal collection structure, but rather collection types that can themselves hold other collection types ... and so it is in Perl. Indeed - Perl has two collection types; you'll us a list if you want to look something up by position number, and perhaps to sort it, and you'll use s hash if you want to look something up by a key - perhaps a string. And you can set up a table with the rows indexed by a number (listish) and the columns indexed by a string (hashish) if you like.

I set up an example in Perl, using a list of lists, yesterday - see [full source] - and always in Perl (!!) the setup was short and a little hard for the newcomer to follow:
  while ($line = <FH>) {
  my @fields = split(/\t/,$line);
  push @records,\@fields;

So that's each line being read, split, saved into a temporarily named list (that my is vital!) and then added onto the end of the list of all the records so far.

A further example - [full source code] - set up a hash of lists, where each line is keyed by the value in one of the fields (I chose the first field) but the the columns of data are numbered:
  while ($line = <FH>) {
  my ($place,@fields) = split(/\t/,$line);
  $records{$place} = \@fields;

You'll notice ... Perl has "autovivification" ... in other words, there's no need for you to setup your lists, hashes, scalars ahead of time - they just get set up automatically for you. If you're writing a medium sized to larger program where you're using subs or modules, though, I will advise you to "use strict" so that you don't accidentally reuse a name across a wide scope and introduce unexpected bugs into your program.

Further examples from yesterday -

Various variable types from our Perl review
Changing the behaviour of a hash from our section on tieing
... all covered in public on our Perl for Larger Projects course!
(written 2010-10-21)

Associated topics are indexed as below, or enter for individual articles
P304 - Perl - Tieing
  [2243] Changing a variable behaviour in Perl - tieing - (2009-06-16)
  [2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
  [3409] When variables behave differently - Tie in Perl - (2011-08-28)

P251 - Perl Review
  [2242] So what is this thing called Perl that I keep harping on about? - (2009-06-15)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [3407] Perl - a quick reminder and revision. Test yourself! - (2011-08-26)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)

P217 - Perl - More than Simple Lists and Hashes!
  [43] Hash of lists in Perl - (2004-09-09)
  [293] Course follow-ups - (2005-04-27)
  [1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3105] Adventure with references to lists and lists of references - (2010-12-26)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
  [3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
  [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)

Back to
Santa announcement, 5th December 2010, Melksham
Previous and next
Horse's mouth home
Forward to
Dulwich College Preparatory, and Sevenoaks, Schools
Some other Articles
What are .pid files?
Children, zombies, and reaping processes
Expect in Perl - a short explanation and a practical example
Dulwich College Preparatory, and Sevenoaks, Schools
Setting up a matrix of data (2D array) for processing in your program
Santa announcement, 5th December 2010, Melksham
Lots of ways of doing it in Perl - printing out answers
Increment operators for counting - Perl, PHP, C and others
What will we be teaching in six years?
A list of special method and attribute names in Python
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 - 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: • WEB: • SKYPE: wellho

PAGE: ... ogram.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb