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

Yes it was of pleanty of use. But tell me, am I trying to do the impossible? Here is where I am right now...without the help my comp would go out the window
use warnings; use strict; use Data::Dumper; my $popCt = 4; my %HoH; for (1 .. $popCt) { my ( $refToArrA, $refToArrB, $refToArrC ) = makeArrays(); &gener(); } print Data::Dumper->Dumpxs([\%HoH], [q{*HoH}]); sub makeArrays { my ($upper, $lower); print 'Enter lower bound: '; chomp ($lower = <STDIN>); print 'Enter upper bound: '; chomp ($upper = <STDIN>); my @chroma; my @chromb; my $ranpop; $ranpop = int rand($upper - $lower + 1) + $lower; print $ranpop . "\n"; my @arrA = ( 1 .. $ranpop ); my $limiter = 50; my @holda; my @holdb; while ($ranpop) { my $genea = int rand ($limiter); push(@holda, $genea); my $geneb = int rand ($limiter); push(@holdb, $geneb); $ranpop--; } my @arrB = ( @holda ); my @arrC = ( @holdb ); return \@arrA, \@arrB, \@arrC; } sub gener { my @newind = @$refToArrA; my @newchroma = @$refToArrB; my @newchromb = @$refToArrC; for my $pop ( 1 .. $popCt ) { $HoH{$pop} = { individual => [ @newind ], chromasome1 => [ @newchroma ], chromasome2 => [ @newchromb ], }; } }
In an ideal world I would have 4 populations with 4 different sets of individuals with 4 different sets of chromosome A and B's. At this point I feel like I am going about this all wrong...

I know strict and warnings are returing things...but I don't know what they mean, google only helps so much.

I really appreciate the help, but tell me if I ask the impossible.

Replies are listed 'Best First'.
Re^5: Hash of Hash Redux
by graff (Chancellor) on Nov 01, 2007 at 05:27 UTC
    I know strict and warnings are returing things...but I don't know what they mean, google only helps so much.

    So in other words, you see these three messages and you don't know what they mean?

    Global symbol "$refToArrA" requires explicit package name at /tmp/junk +.pl line 53. Global symbol "$refToArrB" requires explicit package name at /tmp/junk +.pl line 54. Global symbol "$refToArrC" requires explicit package name at /tmp/junk +.pl line 55.
    You can figure out that lines 53, 54 and 55 are the first three lines after "sub gener". Now, you do have to read a bit about what "my" does in perl, so that you can figure out that in your main "for" loop (over "1 .. $popCt"), the three variables declared there with "my (...)" are not visible (are inaccessible, do not exist) outside the scope of the for loop. Those variables need to be passed to the "gener()" sub as parameters, so that the sub will have access to them, because the "gener" sub is defined outside the scope of the for loop. So do it like this:
    for ( 1 .. $popCt ) { my ( $refToArrA, $refToArrB, $refToArrC ) = makeArrays(); &gener( $refToArrA, $refToArrB, $refToArrC ); } # ... sub gener { my ( $refToArrA, $refToArrB, $refToArrC ) = @_; ... }
    That will get rid of the syntax errors (which were imposed because you have "use strict;" at the top). I don't know whether the script does exactly what you want, but it does run, and produces output in a coherent way.
      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 ], }; } }
        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.)