in reply to So close...
When I execute your code, I get warnings like this:
(line numbers may differ from yours a bit).splice() offset past end of array at foo.pl line 146. splice() offset past end of array at foo.pl line 147. splice() offset past end of array at foo.pl line 158.
It basically means that you try to delete an array element that doesn't exist. Which means that at some point the logic that calculates the index $removed is incorrect.
Maybe you could just write a few lines of comment to each function to describe in your own words what it should do, then we can compare that to what it does.
A few suggestions regarding your code:
sub makeArrays { $lower = 10; $upper = 20; my @chroma; my @chromb; my $ranpop; $ranpop = int rand($upper - $lower + 1) + $lower; # @arrA and @arrD hold the same values, and # aren't modified - why do you need them both? # what is their purpose? my @arrA = ( 1 .. $ranpop ); my @arrD = (1 .. $ranpop ); my $limiter = 50; # you can write that shorter: # my @holda = map { int rand($limiter)} 1 .. $randpop; # my @holdb = map { int rand($limiter)} 1 .. $randpop; my @holda; my @holdb; while ($ranpop) { my $genea = int rand ($limiter); push(@holda, $genea); my $geneb = int rand ($limiter); push(@holdb, $geneb); $ranpop--; } # there's no need to copy these arrays # you can return \@holda and \@holdb directly. my @arrB = ( @holda ); my @arrC = ( @holdb ); return \@arrA, \@arrB, \@arrC, \@arrD; }
You tend to create way too many unnecessary variables that do nothing but clutter up your code (and possibly slow it down):
you can write that asmy $childa = $dada; my $childb = $momb; push @chroma, $childa; push @chromb, $childb;
push @chroma, $dada; push @chromb, $momb;
|
|---|