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
DWIM is Perl's answer to Gödel
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.