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); }


In reply to Duplicate Results from Subroutine by zenrhino

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.