in reply to Re: Duplicate Results from Subroutine
in thread Duplicate Results from Subroutine

Daaaang.

That's like five lessons for the price of one.
I did bang my head against ways to generalize the subroutines not using @numlist, but figured asking two questions at once would be selfish.

So if I have this right, @_ holds the list of parameters passed to the abv subroutine, and we know it gets passed as a list because it ends up as my (@numlist_args) and not my @numlist_args which would pass a scalar. So then the foreach loop works on whatever is passed as paramaters.

I took your help and also applied it to the std_dev subroutine which had the effect of letting me get rid of all of the hardcoded variables that I had originally and making the subroutines easier to read.

Thank you very much!

-Clint

  • Comment on Re^2: Duplicate Results from Subroutine

Replies are listed 'Best First'.
Re^3: Duplicate Results from Subroutine
by Koshatul (Initiate) on May 24, 2013 at 04:27 UTC
    So if I have this right, @_ holds the list of parameters passed to the abv subroutine.
    You got it, here's a version that uses the special variables $_ and @_
    sub abv { my $avg = avg(@_); my @cands = (); foreach (@_) { ($_ > $avg) and push(@cands, $_); } return @cands; }
    I flicked the if statement into a logical and, for each iteration of the foreach $_ will be the same as $cand was in the original function.
      Or, simplified by grep:
      sub abv { my $avg = avg(@_); return grep $_ > $avg, @_; }
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ