in reply to Re^5: Hash of Hash Redux
in thread Hash of Hash Redux

I fixed my issue. With a simple modification
my ( $refToArrA, $refToArrB, $refToArrC ) = makeArrays(); $counter++; &gener( $refToArrA, $refToArrB, $refToArrC ); ... sub gener { my ( $refToArrA, $refToArrB, $refToArrC ) = @_; my @newind = @$refToArrA; my @newchroma = @$refToArrB; my @newchromb = @$refToArrC; for my $pop ( 1 .. $popCt ) { $HoH{$counter} = { individual => [ @newind ], chromasome1 => [ @newchroma ], chromasome2 => [ @newchromb ], }; } }

Replies are listed 'Best First'.
Re^7: Hash of Hash Redux
by graff (Chancellor) on Nov 03, 2007 at 04:10 UTC
    I want a hash with 4 populations, 4 sets of ind, 4 sets of chroma, 4 sets of chromb.

    You mean something like this?

    [ { individual => [ 1 .. N ], # N is population size chromasome1 => [ c1_1 .. c1_N ], # c1_x is an int between 0 and 50 chromasome2 => [ c2_1 .. c2_N ], # c2_x is an int between 0 and 50 } { # same hash elements as above, but different N and c* values } { # same hash elements as above, but different N and c* values } ]
    That's an array of four hashes, where each hash contains a matching set of three keys. The values assigned to the hash keys are arrays of numbers. (It's an "AoHoA".)

    The "individual" hash key and array seems unnecessary, because it's just the ordered set of integers from 1 to N, so I'd be inclined to leave it out, and just keep the chromasome arrays.

    I would also prefer to reduce the number of times I have to type input strings to the running script -- ideally, I'd rather take parameters as command line args via @ARGV, but since you want four pairs of numerics, it makes more sense to ask for four pairs of numerics (not eight separate prompts for single values). If the following doesn't do what you want, try to describe your intended structure more clearly:

    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $popCt = 4; my @whole_thing; for (1 .. $popCt) { my ( $c1, $c2 ) = makeArrays( $_ ); push @whole_thing, { chromasome1 => $c1, chromasome2 => $c2 }; } print Dumper(\@whole_thing); sub makeArrays { my ( $counter ) = @_; print "Enter 'lower upper' bounds for population $counter: "; my ($lower, $upper) = split " ", <STDIN>; my $ranpop = int rand($upper - $lower + 1) + $lower; print $ranpop . "\n"; my $limiter = 50; my @arrA; my @arrB; while ($ranpop) { my $genea = int rand ($limiter); push(@arrA, $genea); my $geneb = int rand ($limiter); push(@arrB, $geneb); $ranpop--; } return \@arrA, \@arrB; }
    In the script you posted earlier, the problem was that you were calling "gener()" four times, and each time it simply assigned a new values to the same set of hash keys in your global %HoH; you didn't make four separate places to store the results of the four iterations. (That's where it's handy to use an array rather than a hash, so you can just push new stuff onto it.)
      I will give that shot thanks for the input!