Deal with the problem a little at a time. First bit is loop over the elements (with strictures, declarations and initialisation thrown in):
use warnings; use strict; my @times = (5, 10, 20, 25, 30, 40, 45, 50, 55, 65, 75, 80, 90); my @times1; my @times2; for my $elmt (@times) {
Then deal with the first case (and the special case for the first element):
if (! @times1 || $elmt - $times1[-1] >= 10) { push @times1, $elmt; next; }
Then deal with the second case (and again the special case for the first special element):
if (! @times2 || $elmt - $times2[-1] >= 10) { push @times2, $elmt; next; }
Then deal with the exception (and close the loop body):
push @times1, $elmt; }
Finally print the result:
print "Times1: @times1\n"; print "Times2: @times2\n";
Prints:
Times1: 5 20 30 40 50 65 75 90 Times2: 10 25 45 55 80
In reply to Re: array splitting
by GrandFather
in thread array splitting
by CColin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |