#!/usr/bin/perl our $DecayParam= 0.5; our $NoChoice= "nan"; sub computeavg { my $sum=0; my $sumn=0; my $decayed=1.0; my @list= reverse(@_); # check decaying. not sure if this is correct. foreach my $v (@list) { if ($v ne $NoChoice) { $sumn+= $decayed; $sum+=$v * $decayed; $decayed*= $DecayParam; } } return ($sumn>=1) ? ($sum/$sumn) : $NoChoice; } my $v= computeavg( 1,2,3,4,5 ); print "value is $v\n";