my @items = (1,5,6,3,5,2,4); my $index = 0; sub do_next { do_something($items[$index]); $index = ($index+1) % @items; }