#!/usr/bin/perl use warnings; use strict; use Data::Dump; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my $i = 1; my $sixindex = 0; my @copy = @$aref; if (scalar(@$aref) >= 2) { foreach my $x (0..$#{$aref}) { print "pass $x\n"; dd $aref; dd \@copy; if (defined($aref->[$x])) { if ($aref->[$x] == 6) { $flag = 1; $sixindex = $x; next; } if ($flag) { unless ($aref->[$x] == 7) { $i++; next; } elsif ($aref->[$x] == 7 ) { print "splice $sixindex, ",($i+1),"\n"; splice(@copy, $sixindex, $i + 1); $flag = 0; $i = 1; $sixindex = 0; next; } } } } } dd \@copy; __END__ pass 0 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] pass 1 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] pass 2 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] pass 3 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] pass 4 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] splice 1, 4 pass 5 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 1, 6, 99, 99, 7] pass 6 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 1, 6, 99, 99, 7] pass 7 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 1, 6, 99, 99, 7] pass 8 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 1, 6, 99, 99, 7] pass 9 [1, 6, 2, 2, 7, 1, 6, 99, 99, 7] [1, 1, 6, 99, 99, 7] splice 6, 4 [1, 1, 6, 99, 99, 7] #### #!/usr/bin/perl use warnings; use strict; use Data::Dump; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my $i = 1; my $sixindex = 0; my $spliced = 0; my @copy = @$aref; if (scalar(@$aref) >= 2) { foreach my $x (0..$#{$aref}) { print "pass $x\n"; dd $aref; dd \@copy; if (defined($aref->[$x])) { if ($aref->[$x] == 6) { $flag = 1; $sixindex = $x; next; } if ($flag) { unless ($aref->[$x] == 7) { $i++; next; } elsif ($aref->[$x] == 7 ) { print "splice ",$sixindex-$spliced, " ",($i+1),"\n"; splice(@copy, $sixindex-$spliced, $i + 1); $spliced += $i + 1; $flag = 0; $i = 1; $sixindex = 0; next; } } } } } dd \@copy; #### #!/usr/bin/perl use warnings; use strict; use Data::Dump; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my @copy; if (scalar(@$aref) >= 2) { foreach my $x (@$aref) { dd $aref; dd \@copy; if (defined($x)) { if ($x == 6) { $flag = 1; next; } if ($flag) { next unless $x == 7; $flag = 0; next; } print "push \@copy,$x\n"; push @copy, $x; } } } dd \@copy; #### #!/usr/bin/perl use warnings; use strict; use Data::Dump; ######## my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]; my $flag = 0; my @copy; if (scalar(@$aref) >= 2) { foreach my $x (@$aref) { dd $aref; dd \@copy; if (defined($x)) { if ($x == 6 .. $x == 7) { next; } print "push \@copy,$x\n"; push @copy, $x; } } } dd \@copy;