Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: create hash names dynamically

by cool (Scribe)
on Jun 11, 2007 at 11:46 UTC ( #620469=note: print w/replies, xml ) Need Help??


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

Hi cdarke,

Could you please explain how this line is working?


push @hashes,{}

You are not using $num values after declearation? I tried, this part of code is creating hashes. But with push, how? Curlies are creating a hash for every iteration but with what hashname? how? As push is used to add scalars to the end of an array!! I tried Perldoc but didn't get any explaination for this. So whats magic here? Please help me understand?

Replies are listed 'Best First'.
Re^3: create hash names dynamically
by citromatik (Curate) on Jun 11, 2007 at 12:33 UTC
    {} creates an empty reference to an anonymous hash. Then,
    for my $num (@a) { push @hashes,{} }

    Reads: For each element in the array @a, create a reference to an empty hash and store it in the array @hashes

    It could be written more compact as in:

    push @hashes,{} for (@a);

    or

    $hashes[$_-1]={} for (@a);

    Hope this helps!

    citromatik

Re^3: create hash names dynamically
by Util (Priest) on Jun 11, 2007 at 12:49 UTC

    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:

Re^3: create hash names dynamically
by cdarke (Prior) on Jun 11, 2007 at 13:57 UTC
    The others have explained it well (I had to nip out to get my car fixed - don't tell my boss).
    The push adds a new element to an array, I guess you know about that.
    The braces {} are called composers that create a hash and return a reference to it. In this case the hash is empty, but we could have placed a key=>value list(s) inside. So push is just putting a reference to the hash into the array.
    The hashes do not have a name, they are (as others have said) anonymous. Which is great, because it means we don't have to stretch our imagination to think up umpteen hash names.
    The memory for the hash will be freed up once the array itself is freed, or assigned to something else (there's a bit more to it, try searching for "Perl reference counting" if this concerns you).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2020-02-22 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (98 votes). Check out past polls.

    Notices?