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

In reply to Re^7: Hash of Hash Redux by graff
in thread Hash of Hash Redux by BioNrd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.