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
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.