This won't work if the list has a six not followed somewhere by a seven, for example with a list like:
my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7, 5, 3, 6, 5, 88];
the last three digits are omitted from the result, although they are not in a 6..7 range.
| [reply] [d/l] |
Sure, but I was explaining algorithm for the OP and was deliberately avoiding that part of the discussion, because normally, one does not allow e.g. a left bracket without a right bracket before even letting the data through to the processing phase - that is a "syntax error" in my book. So in my example, if the flag is still turned on after the loop I would terminate with an error message, unless the OP specifically stated that an unterminated 6 is allowed, in which case I would save what isn't being pushed into a separate array and push it at the end if the flag is still turned on i.e.:
use Data::Dumper;
my $allow_loose6 = 1;
my $aref = [1, 6, 2, 2, 7, 1, 6, 99, 99, 7];
my @new = ();
my @loose = ();
my $flag = 0;
for (@$aref) {
$flag = 1 if (!$flag and $_==6);
if (!$flag) {
push @new, $_;
}
else {
push @loose, $_;
}
if ($flag and $_==7) {
$flag = 0;
@loose = ();
}
}
$aref = \@new;
if ($allow_loose6) {
push @$aref, @loose;
}
elsif (@loose) {
die "Unterminated 6 detected";
}
print Dumper $aref
| [reply] [d/l] |
for (@$aref) {
$flag = 1 if $_== 6;
push @new, $_ unless $flag;
$flag = 0 if $_== 7;
}
| [reply] [d/l] [select] |