in reply to resetting a foreach loop!

Hi lunette,

pryrt gave you the solution, but there is one point to be noted and that you need to understand.

In general, when you write a subroutine, it is usually best if the subroutine can work only on parameters passed to it, and not on global variables.

In your code, you pass the data to the subroutine as an argument:

$average = average(@array);
but you don't use the parameter within the average subroutine, but use the global array variable. In other words, you made half of the work needed, but not the whole thing. Here you might want to do something like this:
sub average { my @local_array = @_; if (@local_array) { # ... }
There are some exceptions, sometimes (rarely), there is a good reason to work directly on a global array or hash within a subroutine (for example for performance reasons if the array or hash is really very large), but, generally speaking, it is most of the time better for subroutines to work on copies of arguments passed to them.