while (my (@positions) = $i->()) { my @tmp; my $cur = 0; for my $pos (@positions) { push @tmp, @arr[$cur..$pos-1]; $cur = $pos; push @tmp, 'N'; } push @tmp, @arr[$cur..$#arr]; print(join(',', @tmp), "\n"); }