Here's a working version. I changed the temperature ranges slightly to take into account unlimited decimal places and the possibility of temperatures under 15.
use strict;
use warnings;
my $rows = 5;
my @temps = ('mild','warm','very warm','hot','very hot','extremely hot
+');
my ($range, $lrange, $start, $c, @data, @results);
# Creating nested arrays with data
while (<DATA>) { chomp; push @data, [split(/ /)]; }
$lrange = -1;
for (@data) {
$range = getrange(@$_[1]);
if ($range == $lrange) {
if (++$c == $rows) {
push (@results, [$start, @$_[0], $temps[$range]]);
$lrange = -1; $c = 0;
}
}
else {
$start = @$_[0]; $lrange = $range; $c = 1;
}
}
# Printing results, tab delimited
for (@results) { print join("\t", @$_) . "\n"; }
sub getrange {
return 5 if $_[0] >= 40;
return 0 if $_[0] < 20;
return int($_[0] / 5 - 3);
}
__DATA__
1-1-2004 33.95
2-1-2004 30.34
3-1-2004 31.50
4-1-2004 32.50
5-1-2004 33.50
6-1-2004 90.00
7-1-2004 51.45
8-1-2004 52.56
9-1-2004 56.23
10-1-2004 50.54
11-1-2004 52.43
-
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.