http://www.perlmonks.org?node_id=702380

gingaloon has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody, I have an array that I wish to iterate over and each time insert an index. This I can do. My problem is that sometimes I wish to iterate twice, inserting in two positions. This I can also do (see below). However, I cannot come up with a nice way of potentially doing both in a clean piece of code. I have one for the former and one for latter.

Can anyone point me in the right direction? Thanks

my @arr=qw/A B C D E F/; print join (",",@arr),"\n"; for (my $i=0;$i<=$#arr+1;$i++) { for (my $j=$i+1;$j<=$#arr+2;$j++) { my @tmp=@arr; splice @tmp,$i,0,'N'; splice @tmp,$j,0,'N'; print join (",",@tmp),"\n"; } }