Hi,
I am writing a genetic algorithm which allows users to seed the program run with some number of "critters". I want to use this number to generate a string (the "gene") for each "critter", then associate the name of each critter and its gene in a hash. No problem.
However, when I then wish to create an array to hold the round-by-round history of each "critter's" behavior, I am stuck. I -think- I want to create the array on the fly and then store it in a hash using the critter's name as a keyword. I seem to be able to do this, so long as I turn OFF "use strict;" (see code below) I modeled this "behavior" by using random $x to represent some sub-routine output.
I hope to be able to end up with a
hash {
Critter1 -> ARRAY_Critter1
Critter2 -> ARRAY_Critter2
.... etc
}
where each ARRAY_CritterX has some series of values: (1,6,12,34,8,14...etc)
The question is two-part: 1. What am I doing wrong??? and 2. How can accomplish this using strict pragma?
Thanks in advance. John
I am writing a genetic algorithm which allows users to seed the program run with some number of "critters". I want to use this number to generate a string (the "gene") for each "critter", then associate the name of each critter and its gene in a hash. No problem.
However, when I then wish to create an array to hold the round-by-round history of each "critter's" behavior, I am stuck. I -think- I want to create the array on the fly and then store it in a hash using the critter's name as a keyword. I seem to be able to do this, so long as I turn OFF "use strict;" (see code below) I modeled this "behavior" by using random $x to represent some sub-routine output.
I hope to be able to end up with a
hash {
Critter1 -> ARRAY_Critter1
Critter2 -> ARRAY_Critter2
.... etc
}
where each ARRAY_CritterX has some series of values: (1,6,12,34,8,14...etc)
The question is two-part: 1. What am I doing wrong??? and 2. How can accomplish this using strict pragma?
Thanks in advance. John
Code:
#------------------------------------------------
my $i=5; #User-selected number of critters
my $name;
my %hash=(); #Hash of "Critter->ARRAY of Behaviors"
print "KEY \tVALUE\n";
while ($i>0) {
my $x = int(rand(20)); #Simulate Behavior results
$name="Critter"."$i"; #Generate each Critter
my $name_ref= "$name";
my $array_name="$name"."history";
push(@$array_name,$x);
print "$array_name -> @$array_name[0]\n"; #Print first value in array to verify this works
$hash{$name_ref}=$array_name->[0];
$i--;
}
#-----------------------------------------------
#Now print out the hash and look at it
while (my ($key, $value) = each (%hash)) {
print "$key \t$value\n";
}