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

Re: create hash names dynamically

by cdarke (Prior)
on Jun 11, 2007 at 08:26 UTC ( [id://620439]=note: print w/replies, xml ) Need Help??


in reply to create hash names dynamically

As Corion said, you probably don't want to do that. You don't give any details of how you want to use these hashes, so it is difficult code that is useful. If you wanted an array of hashes from the array of numbers that you gave:
my @a = (1, 2, 3, 4); my @hashes; for my $num (@a) { push @hashes,{} }
You access the hashes, let's say the second hash with a key called 'key', like this:
$hashes[1]{key} = 'some value'; print "$hashes[1]{key}\n";
Much more elegant that creating dynamic variables.

Replies are listed 'Best First'.
Re^2: create hash names dynamically
by cool (Scribe) on Jun 11, 2007 at 11:46 UTC
    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?

      {} 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

      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:

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://620439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found