in reply to Re^2: Ignore a range of numbers ina List
in thread Ignore a range of numbers ina List

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

One world, one people

Replies are listed 'Best First'.
Re^4: Ignore a range of numbers ina List
by Laurent_R (Canon) on Jun 25, 2017 at 11:05 UTC
    Yes, you're right, the OP did not specify this possibility, so this may or may not be needed. But taking this possibility into account was what made the solutions suggested by other monks slightly more complicated.

    Note that you probably don't need to test $flag in the statements where you change its value. For example the loop of your first solution could be simplified as follows:

    for (@$aref) { $flag = 1 if $_== 6; push @new, $_ unless $flag; $flag = 0 if $_== 7; }
      ... the OP did not specify ...

      pr33 offers some further specification examples here. (Sneaking up on a test-driven methodology... :)


      Give a man a fish:  <%-{-{-{-<

      Yes you are right - guess I didn't check whether that optimisation was possible, I was just wondering why the most obvious approach wasn;t being suggested.

      One world, one people