I was hacking up a graphics display program, and I needed to add a moving-average line along with a data plot. Came up with this simple little few-liner.
my $AVERAGE_OVER = 10; # number of periods for moving average ... my @averages = do { my $sum = 0; my @summers = (); map { $sum += $_; push @summers, $_; $sum -= shift @summers if @summers > $AVERAGE_OVER; $sum / @summers; } @data; # THIS IS THE INPUT DATA };

Replies are listed 'Best First'.
Re: moving average
by I0 (Priest) on Dec 25, 2000 at 17:17 UTC
    It may not matter for a graphics display but if @data values are floating, beware of accumulated round off errors.
Re: moving average
by extremely (Priest) on Dec 25, 2000 at 12:38 UTC
    Now that is just the best use of a "do block" I've seen all year. I'd have done that as a sub, everytime. You win the "Baby Bear" award for the year. =) It's not too hot, it's not too cold, that code is exactly right.

    --
    $you = new YOU;
    honk() if $you->love(perl)