... # get $newvalue from somewhere addHist( \%history, $histcount, $newvalue ); ... # deside we want the $n'th previous value my $last_nth = getHist( \%history, $histcount, $n ); ...