Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Out of memory during array extend - Perl

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 20:18:28)

 
Associated topics are indexed under
P208 - Perl - Lists
P211 - Perl - Hashes

Back to
Why I remember East Grafton
Previous and next
or
Horse's mouth home
Forward to
Perl Socket Programming Examples
Some other Articles
Romeo and Julie
Progress Bar Techniques - Perl
Transition
Perl Socket Programming Examples
Out of memory during array extend - Perl
Why I remember East Grafton
Quietly putting prices up
It must be nearly Christmas
Alastair Darling helps make paperwork and confusion
Book now for 2009
1975 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 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).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho