Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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!
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [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)
  [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)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
  [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)


Back to
Where do businessmen stay in Melksham?
Previous and next
or
Horse's mouth home
Forward to
Perl - a quick reminder and revision. Test yourself!
Some other Articles
A review of the Summer Sunday extra trains on the TransWilts line
When variables behave differently - Tie in Perl
Journey home by public transport for a Bank Holiday
Perl - 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 Busway
Last chance this summer - Swindon and North Wiltshire to Weymouth by through train
That spec is a kingfisher ...
Open Source Training Schedule - learn a programming language - in Autumn 2011 or 2012
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 http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3406_Not ... Perl-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat