vipinh has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I need a hash to get its key names created in this fashion :
a constant string concatenated with a variable whose value
will change dynamically, for eg :

$seg{age1}--- where $seg is the hash, 'age' is the constant
string, and 1 is generated from a varaible, so actually
i want to write it like :

$seg{age$count}

Is this possible in Perl ? Really need some help right now.
thanks
  • Comment on dynamically creating the name of a hash key-name

Replies are listed 'Best First'.
Re: dynamically creating the name of a hash key-name
by merlyn (Sage) on Oct 01, 2000 at 15:03 UTC
Re: dynamically creating the name of a hash key-name
by 2501 (Pilgrim) on Oct 01, 2000 at 18:19 UTC
    If I remember correctly, keys are always treated like strings and can't be considered an integer, etc. With that in mind, just think of what you could do to a string and you should be all right. For example:

    for($I=0;$I<3;$I++){ $foo{"word".$I}= "This is word $I\n"; }

    You could then reference foo with $foo{word0}, $foo{word1}, and $foo{word2}
Re: dynamically creating the name of a hash key-name
by extremely (Priest) on Oct 02, 2000 at 00:14 UTC
    You know, if the counts are linear and start at 1 or 0 you might want to consider making a Hash of Arrays. Then you could write that: $seg{age}[$count]="bleagh"

    That migh be especially nice if you later want to work thru all the $counts under "age".

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: dynamically creating the name of a hash key-name
by Anonymous Monk on Oct 02, 2000 at 00:14 UTC
    I have a related question:

    If I want to create 20 separate hashes dynamically, each of which has the same keys but different values, I want to have

    $hash$numericid{$key} = $value;
    
    How to get each has to be named
    %hash1, %hash2, etc?
    
    -clay

      One way to do pretty much what you're asking for relates to a reply above: what you can do is have an array of anonymous hashes, of the form:

      my @array_of_hashes = ( {hashkey1=>$hashval1, hashkey2=>$hashval2}, {hashkey1=>$hashval1, hashkey2=>$hashval2} ... );

      You access the individual hashes with syntax like:

      # to modify what's in the second hash $array_of_hashes[1]->{hashkey1} = "ay carumba!"; # $array_of_hashes[1]{hashkey1} would also work! # to iterate over the hashes foreach my $hashref (@array_of_hashes) { # $hashref is now a reference to one of the hashes in @array _of_hashe +s # we need to de-reference to go through the keys of the hash foreach (keys %$hashref) { print "$_ => ", $hashref->{$_}, "\n"; } }

      HTH

      Philosophy can be made out of anything -- or less

      To do this, you would need a piece of code like this:

      #!/usr/bin/perl my ($i, @hasharray, $x); # create an array of hashnames (hash0..hash9) for ($i=0; $i<10; $i++) { push @hasharray, "hash$i"; } $i = 10; foreach (@hasharray) { $x = $_; #assign key "key" and value "value$i" to %hashx $$x{key} = "value$i"; $i++; } print "$hash7{key}\n"; #would print "value17"
      But don't use strict or -w here! Strict would prevent using a string as a hashref ($$x{key} = "value$i") and -w would warn us that %hash7 was used only once...

      So you see, it is possible, but I think it's not wise...

      HTH

      Jouke Visser, Perl 'Adept'