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

create hash names dynamically

by phemal (Sexton)
on Jun 11, 2007 at 06:40 UTC ( #620420=perlquestion: print w/ replies, xml ) Need Help??
phemal has asked for the wisdom of the Perl Monks concerning the following question:

Hi floks, I want a quick help on creating hash names dynamically
@a = qw (1 2 3 4);
Now i want to create hash names dynamically depending upon the elements in array ex : %hash_1,%hash_2... Thanks

Comment on create hash names dynamically
Download Code
Re: create hash names dynamically
by Corion (Pope) on Jun 11, 2007 at 06:46 UTC
      Hi Corion,

      Felt really good by reading the one you have sent "Why it's stupid to `use a variable as a variable name" Some times even when we are programming we unknowingly or knowingly do these kind of stuff for which we the Client in some cases losess his money and in turn you lose your reputation.

      Always important is Client/Customer satisfaction, we cannot lose one because of this.

      Its was also a kind of learning thing the way it was well explained what consequnces can occur during the phase or latter ..... Thank You very much

      Work Hard Party Harderrr!!
      Sushil Kumar
Re: create hash names dynamically
by cdarke (Prior) on Jun 11, 2007 at 08:26 UTC
    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.
      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,

        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);


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

        Hope this helps!


        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://620420]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-07-10 22:11 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (217 votes), past polls