|
Autovivification - the magic appearance of variables in Perl
Here's a one line perl program that does nothing but set up a variable ... except that it does a lot!
$p[7]{john}{paul}[9] = 1;
__END__
A list called @p. Elements 0 to 6 are null, and element 7
is a reference to a hash ...
That hash contains a single member, key is "john" and the
values it holds is a reference to another hash ...
THAT hash contains a single member with a key of "paul" and
it contains a reference to a list ...
And that list has elements 0 to 8 being null, and element
nine containing the value 1.
AND THAT WAS ALL DONE AUTOMATICALLY by Perl's Autovivification
capability!!
If you assign to a variable in Perl and it does not already exist, the Perl creates it dynamically - and that implies that it also creates all the other necessary structures as well. My oneliner above took 10 lines to describe in plain English!
This is both powerful and dangerous. Something as simple as
$phone[1225708225] = 1;
will give you "out of memory" as a list of over one billion elements is calledup. But do remember that
$phone{1225708225} = 1;
will create a hash with just one member, and THAT is what you should use for data with sparsely distributed numeric keys (written 2008-01-21)
Associated topics are indexed under 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) [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) [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) [293] Course follow-ups - (2005-04-27) [43] Hash of lists in Perl - (2004-09-09)
561e
Some other Articles
Downloading data for use in Excel (from PHP / MySQL)Python - formatting objectsPictures you can use - for free - from our libraryKeeping staff up to date on hotel room statusAutovivification - the magic appearance of variables in PerlPerl, PHP or Python? No - Perl AND PHP AND Python!Summer Ball at Bowood - Saturday 12th July 2008Buses from Well House Manor, Melksham, to BathHandling Binary data (.gif file example) in PerlExtracting information from a file of records
|
4088 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 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).
|
|