Thank you again for your replies. My code now looks as follows...and works...

print "\n\nEnter numbers, one number per line.\nEnter a blank line whe +n all values have been entered.\n"; my @values=(); while ($val=<>) { chomp($val); if ($val != "0") { @values=(@values,$val); } elsif ($val eq "0") { print "Please do not enter values of zero.\n"; } elsif ($val eq "") { last; } } my $arraysize=@values; print "\nYou have entered $arraysize values\n\n"; # &minimum(@values); # &maximum(@values); # &maximum(@values,$arraysize); my $min = minimum(@values); my $max = maximum(@values); my $average = average(@values); print "The minimum you have entered is $min\n\n"; print "The maximum you have entered is $max\n\n"; print "The average of the numbers you entered is $average\n\n"; sub maximum { @args = @_; my $max = $args[0]; foreach $i (@args) { if ($i > $max) { $max = $i; } } return $max; } sub minimum { my @things = @_; my $min = $things[0]; foreach my $z (@things) { if ($z < $min) { $min = $z; } } return $min; } sub average { @stuff = @_; my $sum = 0; ($sum+=$_) for @values; my $average = $sum / $arraysize; return $average; }

I'm elated that it works now, but I'm still not quite understanding how part of it works (which is just as important to me as having it work). We were taught in class to call subroutines by the method I have shown above which is commented out. I'm especially confused by this, because for the "Average" subroutine, I figured I would need to pass "@values" as well as "$arraysize" for it to work, but it seems to work without "$arraysize". $arraysize isn't a global variable in the program because I used  my $arraysize, right? So how can the subroutine still work without passing it to that?

I feel like there's some simple concept here that I am just not able to grasp or something, and it's extremely aggravating.

Another question I have now, is if the subroutines return $min $max $average, why do I need those three lines

my $min = minimum(@values); my $max = maximum(@values); my $average = average(@values);
that set those variables to something again? Shouldn't they just work in the print statements after they're returned from the subroutines?


In reply to Re^3: Passing an array to a subroutine help?! by Douglas0028
in thread Passing an array to a subroutine help?! by Douglas0028

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.