Use a hash if you want a sparse list in Perl!
If you set up a
list in Perl and don't fill all the members from zero up, that's OK but all the missing elements will actually use up some memory as they'll point to an "undef" value. So this happens:
Dorothy:ppcsrd08 grahamellis$ perl
$num[1223776336] = 0;
Out of memory during array extend at - line 2.
Dorothy:ppcsrd08 grahamellis$
That's attempting to create over 1 billion members of a list ... which would take an awful lot of memory.
You can, however, use a
hash instead:
Dorothy:ppcsrd08 grahamellis$ perl
$num{1223776336} = 0;
Dorothy:ppcsrd08 grahamellis$
In a hash you name rather than number your elements ... but an element can be named with any scalar, thus allowing to produce a sparse collection - one in which you don't have to predefine a whole lot of keys in order to reach the one that you need.
Lists are written with square brackets - [ and ] - round each member, but members of a hash are written with curly braces - { and }. If you refer to the whole of a list, use the "@" character, but if you refer to the whole of a hash, use a "%".
You can learn a lot more about lists and hashes on our
Perl Programming Course ... I'm teaching a private variant this week in Cambridge with some extra topics such as a little bit of socket programming ... see the next post ...
(written 2008-12-02, updated 2008-12-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P211 - Perl - Hashes [240] Conventional restraints removed - (2005-03-09)
[386] What is a callback? - (2005-07-22)
[738] (Perl) Callbacks - what are they? - (2006-05-30)
[930] -> , >= and => in Perl - (2006-11-18)
[968] Perl - a list or a hash? - (2006-12-06)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1856] A few of my favourite things - (2008-10-26)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3106] Buckets - (2010-12-26)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
Some other Articles
Romeo and JulieProgress Bar Techniques - PerlTransitionOut of memory during array extend - PerlWhy I remember East GraftonQuietly putting prices upIt must be nearly ChristmasAlastair Darling helps make paperwork and confusionBook now for 2009