Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: create hash names dynamically

by Util (Priest)
on Jun 11, 2007 at 12:49 UTC ( [id://620489]=note: print w/replies, xml ) Need Help??


in reply to Re^2: create hash names dynamically
in thread create hash names dynamically

This code:

foreach (1..3) { push @hashes, {}; }
does the same thing (but faster and cleaner) as this code:
foreach (1..3) { my %any_name; push @hashes, \%any_name; }

At the end of the each loop in the second example, the %any_name hash would normally vanish, but because a *reference* to it is saved, the hash itself still floats in memory, with no name associated with it. Since a reference is a kind of scalar, it can be stored in the array just like any other scalar. The anonymous hash constructor, {}, allows you to avoid any %temp_hash that you did not need to refer to by name.

See also:

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://620489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-19 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found