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 under
P251 - Perl Review [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3407] Perl - a quick reminder and revision. Test yourself! - (2011-08-26)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[2242] So what is this thing called Perl that I keep harping on about? - (2009-06-15)
P217 - Perl - More than Simple Lists and Hashes! [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
[3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
[3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3105] Adventure with references to lists and lists of references - (2010-12-26)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[2877] Further more advanced Perl examples - (2010-07-19)
[2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
[293] Course follow-ups - (2005-04-27)
[43] Hash of lists in Perl - (2004-09-09)
P304 - Perl - Tieing [3409] When variables behave differently - Tie in Perl - (2011-08-28)
[2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
[2243] Changing a variable behaviour in Perl - tieing - (2009-06-16)
Some other Articles
What are .pid files?Children, zombies, and reaping processesExpect in Perl - a short explanation and a practical example Dulwich College Preparatory, and Sevenoaks, SchoolsSetting up a matrix of data (2D array) for processing in your programSanta announcement, 5th December 2010, MelkshamLots of ways of doing it in Perl - printing out answersIncrement operators for counting - Perl, PHP, C and othersWhat will we be teaching in six years?A list of special method and attribute names in Python