Greetings, Monks.
I'm working my way through Learning Perl 6th Ed and doing an expansion of Probs. 1-3 on pp. 78.
My problem is that is I use the @abvlist array as seen on list 16, I get the correct answer. If I call the function directly as on line 17, I get a duplicate set of the answer. I am at a loss as to why this happens.
I suspect, that as with most things in Perl, it's going to be something simple and small I'm looking right over.
Run as is, I get the following:
The count of numbers in @numlist is 8.
The total of @numlist is 40.
The mean average of @numlist is 5.
The list of numbers in @numlist over the mean is 7,9.
The list of numbers that will probably be duplicates in @numlist over the mean is 7,9,7,9.
The standard deviation of numbers in @numlist is 2.
Finished in 0.1s
I get the same results from the commandline in powershell as I do from the build run in ST2.
I've searched for "duplicate results from subroutine" and "duplicate results from function" and didn't see anything.
All the best,
-ZR
#!/C:\Perl64\bin\perl.exe
use strict;
use warnings;
my @numlist = qw(2 4 4 4 5 5 7 9);
my $numlist_avg = avg(@numlist);
my @cands;
my @abvlist = abv(@numlist);
my $sigma = std_dev(@numlist);
my @squares;
my $len = $#numlist + 1;
print "The count of numbers in \@numlist is ", $#numlist + 1, "\.\n";
print "The total of \@numlist is ", total(@numlist), "\.\n";
print "The mean average of \@numlist is ", avg(@numlist), "\.\n";
print "The list of numbers in \@numlist over the mean is ", join(',',
+ @abvlist), "\.\n";
print "The list of numbers that will probably be duplicates in \@numli
+st over the mean is ",
join(',', abv(@numlist)), "\.\n";
print "The standard deviation of numbers in \@numlist is ", $sigma, "\
+.\n";
sub total
{
my $endresult = 0;
foreach my $x (@numlist) {
$endresult += $x;
}
return $endresult;
}
sub avg
{
my $len1 = $#numlist + 1;
return total(@numlist) / $len1;
}
sub abv
{
foreach my $cand (@numlist) {
if ($cand > $numlist_avg) {
push(@cands, $cand);
}
}
return @cands;
}
sub std_dev
{
my $len2 = $#numlist + 1;
foreach my $num (@numlist) {
my $step1 = $num - $numlist_avg;
my $step2 = $step1 * $step1;
push(@squares, $step2);
}
my $start2 = 0;
foreach my $num2 (@squares) {
$start2 += $num2;
}
return sqrt($start2 / $len2);
}