I remember an old game I used to play on DEC 10 (and VAX and PDP-11) computers - the
original adventure game, perhaps (
[see here]) which was pure text / instruction based and you traveled North, South, East and West through the colossal cave. And there was one are - the maze - where you could get rather lost between a little maze of twisting passages, little maze of twisty passages, little twisty maze of passages, maze of little twisting passages, maze of little twisty passages, maze of twisting little passages, maze of twisty little passages, twisting little maze of passages, twisting maze of little passages, twisty little maze of passages and a twisty maze of little passages!
And I was reminded about that the other day when I was differentiating in Perl between a list of scalars, a list of references to scalars, and a reference to a list of scalars.
A series of scalars - held in a collections that's ordered (numbered from 0 upwards and sortable) is a list in Perl ... and you can create a list of scalars using round brackets:
@numbers = (10,20,30,40);
If you want to hold pointers / references / addresses of scalars rather than the scalars themselves in a list, you'll add an extra \ character:
@again = \(10,20,30,40);
In Perl terms, that looks like a single pointer to the list but it's not - it's an exception to the usual syntax, and it returns a whole list of pointers.
If you're really looking for a single pointer to a whole list, you'll use square brackets:
$further = [10,20,30,40];
So ...
A list of scalars (@numbers):
10 20 30 40
A list of references to scalars (@again):
SCALAR(0x100800f20) SCALAR(0x100800f30) SCALAR(0x100800f40) SCALAR(0x100800f50)
and a reference to a list (of scalars, but we can't tell that from the output) ($further):
ARRAY(0x10082c270)
There's an extended example - showing how hashes and references to hashes and context fits in too -
[here].
(written 2010-12-26, updated 2011-01-03)
Associated topics are indexed under
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)
[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)
Some other Articles
A weighty decisionMy First ChristmasHotel and Training Course prices - the effect of the VAT rise on 4th January 2011BucketsAdventure with references to lists and lists of referencesCatering in Syracuse, the Saigon Cafe, stolen images and ChristmasThank you - and Happy ChristmasAND and OR operators - what is the difference between logical and bitwise varieties?The week before ChristmasLooking ahead and behind in Regular Expressions - double matching