my $n = 3; my $k = 2; sub iterate { my ($curr_n, @values) = @_; for my $curr_k (1..$k) { if ($curr_n == $n) { say join(', ', (@values, $curr_k)); } else { iterate($curr_n +1, (@values, $curr_k)); } } } iterate(1, ());