All pastes #137175 Raw Edit

Miscellany

public text v1 · immutable
#137175 ·published 2006-08-18 04:49 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use warnings;

my @fred = &above_average(1..10);
print "\@fred is @fred\n";
print "(should be 6 7 8 9 10)\n";
my @barney = &above_average(100, 1..10);
print "\@barney is @barney\n";
print "(should be just 100)\n";

sub above_average {
        my (@list) = @_;
        my $average = &average(@list);

        print "@list\n";
        @_ = ();
        print "@_\n";

        foreach (@list) {
                if ($_ > $average) {
                        push @_, $_;
                }
        }
        @_
}

sub average {
        my (@list) = @_;
        my $total;

        foreach (@list) {
                $total += $_;
        }
        $total/@list
}