use Modern::PBP::Perl; my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7, 1, 2, 6, 9, 8, 6, 5]; my @copy; my @stack; (($_ == 6 .. $_ == 7) and push @stack, $_) or (push @copy, $_ and @stack=()) for @$aref; push @copy, @stack; use Data::Dumper; print Dumper \@copy;