oh hai, monks.
I am re-trying my hand at perl and going through the Lllama one more time.
I'm attempting exercise 3 on chapter 4.
here's what I came up with:
-------------------
#!/usr/bin/perl -w
use strict;
sub avg {
my $total;
foreach (@_) {
$total += $_;
}
print "This is the value of \$total: $total \n ";
my $num = (@_);
print "This is the value of \$num: $num \n";
return $total / $num;
}
sub above_avg {
my $tot_avg = &avg(@_);
print "This is the value of \$tot_avg $tot_avg \n";
my @product;
foreach (@_) {
if ($_ > $tot_avg) {
(@product) = $_;
}
}
print "This is the value of \@product @product \n";
return @product;
}
print "Please enter a series of numbers: ";
my @entered = (<STDIN>);
print "Here are the numbers above the average: ", &above_avg(@entered)
+, "\n";
-----------------------------
The assignment is to produce a list of numbers that are above the average of a set of numbers input by the user.
For some reason, my array (@product) doesn't seem to be acting like an array. It only accepts ONE number instead of however many are above the average. Any hints?
Thanks!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.