in reply to Re^2: Passing an array to a subroutine help?!
in thread Passing an array to a subroutine help?!
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
that set those variables to something again? Shouldn't they just work in the print statements after they're returned from the subroutines?my $min = minimum(@values); my $max = maximum(@values); my $average = average(@values);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Passing an array to a subroutine help?!
by chromatic (Archbishop) on Mar 15, 2011 at 21:11 UTC | |
|
Re^4: Passing an array to a subroutine help?!
by Corion (Patriarch) on Mar 15, 2011 at 21:00 UTC |