One possible way would be to do a routine that calculates the next state from the previous one no matter how many 'N's are in the array. That way you just put the number of 'N's at the beginning of the array and call the routine repeatedly until all 'N's are at the end of the array
use warnings;
use strict;
my @arr=qw/A B C D E F/;
my $anzahl=2;
unshift @arr, ('N') x $anzahl;
print join (",",@arr),"\n";
while (@arr= step (@arr)) {
print join (",",@arr),"\n";
}
sub step {
# returns empty list if all marks are already at the end
my @arr= @_;
my $marksatend=0;
my $i= @arr;
#count marks at the end
while ($i) {
if ($arr[--$i] eq 'N') {
$marksatend++;
}
else {
last;
}
}
#find a mark to advance
my $mark=-1;
while ($i) {
if ($arr[--$i] eq 'N') {
$mark=$i;
last;
}
}
#advance
if ($mark<0) {
return ();
}
else {
my $tmp= $arr[$mark];
$arr[$mark]= $arr[$mark+1];
$arr[$mark+1]= $tmp;
if ($marksatend>0) {
splice @arr,-$marksatend;
splice @arr,$mark+2,0, ('N')x$marksatend;
}
return @arr;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.