Thanks for that tip, JavaFan. I used it, and you are so right. It is much faster and my memory doesn't run out! Unfortunately, I did come against another error "Range iterator outside integer range". I haven't quite got that figured just yet.
I didn't use your exact fragment, but I think it is close enough.
#!/usr/bin/perl -l
use strict;
use warnings;
use feature qw(say);
use List::Util qw(sum max);
use lib 'lib';
use Base::Nifty qw(commify);
say "How many children are in the first generation?";
my $generation = <>;
say "How many generations do you want to generate?";
my $generations = <>;
chomp($generation,$generations);
my %generations = (
1 => $generation,
);
my @distribution = (0,0,0,1,1,1,2,2,2,3,3,4,5,6);
for my $gen (1..$generations) {
my $children = 0;
for (1..$generation) {
$children += $distribution[rand(@distribution)];
}
$generation = $children;
last if ($children == 0);
$generations{$gen + 1} = $generation;
}
my $max_length_generation = length(commify(max(keys %generations)));
my $max_length_children = length(commify(max(values %generations)));
for (sort {$a <=> $b} keys %generations) {
printf "%${max_length_generation}s: %${max_length_children}s\n",$_,c
+ommify($generations{$_});
}
The += did a whole lot of good! I haven't used that construction much in the past, so I don't automatically think of it. Hopefully one day it will sink in.
Have a cookie and a very nice day!
Lady Aleena
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.