Not multidimentional arrays - but lists of lists. Much more flexible. Perl!
In Perl, you don't have multidimensional structures as such - you have list and hashes ... and you can have lists of hashes, hashes of lists and hashed of hases too. Which give you almost all of the benefits of multi dimensional structures, but with few of the limitations. And they can be set up in short pieces of code too.
while (<s;>) {
my @hit = split;
push @accesses,\@hit;
}
Each line from a data file named on the command line is read into $_. (<> defaults to reading from a file named on the command line, and in a while statement it's saved into $_ unless the code specifies somewhere else).
The line is split into a list of fields, the separator between the fields being white spaces. (split uses white space and $_ as its defaults).
Onto a list called
@accesses, we add a reference to (i.e. the address of the list
@hit). The very first time through the loop, the list is created and it's automatically extended each time.
On reaching the } at the end of the loop, the name
@hit is released as this was a "my" variable scoped to the block. However, since the actual data is now referenced within the
@accesses list, the memory that it has used will be retained ... resulting in a new location for a new
@hit next time around the loop!
So - having set up that data, how do we access it?
print ${$accesses[100]}[9],"\n";
That's "Look at contents of the 100th element of the
@accesses list and within the list that's pointed to there look at the contents of the 9th element" [but counted in each case from 0 not 1, so really the 10th element of the 101st row].
We could make that a bit easier:
print $accesses[100]->[9],"\n";
The
-> operator has replaced
${---} - it's just a syntactic change, but never the less it helps with followability, and indeed I can also write:
print $accesses[100][9],"\n";
According to the rules of the language, I shouldn't be able to do this ... except that wehn Perl sees two subscripts butted up against each other without any opertor in between them, it assumes the author wanted
-> ... which usually he did!
(written 2011-08-26)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[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)
[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)
Some other Articles
A review of the Summer Sunday extra trains on the TransWilts lineWhen variables behave differently - Tie in PerlJourney home by public transport for a Bank HolidayPerl - a quick reminder and revision. Test yourself!Not multidimentional arrays - but lists of lists. Much more flexible. Perl!Where do businessmen stay in Melksham?Buses on the Cambridge Guided BuswayLast chance this summer - Swindon and North Wiltshire to Weymouth by through trainThat spec is a kingfisher ...Open Source Training Schedule - learn a programming language - in Autumn 2011 or 2012