in reply to Searching in an array of arrays

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

Replies are listed 'Best First'.
Re^2: Searching in an array of arrays
by perl_seeker (Scribe) on Nov 04, 2004 at 09:55 UTC
    Hi Ted! way to go, your code works just fine. I'll need to go through it properly to understand it fully. Your help is much appreciated.

    Cheers,
    perl_seeker:)
Re^2: Searching in an array of arrays
by perl_seeker (Scribe) on Nov 04, 2004 at 11:45 UTC
    Oops! I tested your code by reading the data from a text file to build @data, and it worked fine. While trying to
    run your code with the rest of my code( I have built @data by parsing an XML file using XML::XPath), I'm getting errors similar to this
    Operation `/': no method found, left argument in overloaded package XML::XPath::Literal,right argument has no overloaded magic at C:\..\xmlrecord_parse.pl line 608. Tool completed with exit code 255
    So I've changed this portion of code, but I'm not sure if this is correct :
    return 5 if (int($_[0]) >= 40); return 0 if (int($_[0]) < 20); return ((int($_[0]) / 5) - 3);
    However, the results printed now are:
    1-1-2004 5-1-2004 mild
    Whereas they should be:
    1-1-2004 5-1-2004 mild 10-1-2004 14-1-2004 very warm
    Data
    1-1-2004 15.0 2-1-2004 15.5 3-1-2004 16.5 4-1-2004 17.0 5-1-2004 17.5 6-1-2004 18.0 7-1-2004 18.5 8-1-2004 19.0 9-1-2004 19.5 10-1-2004 25.0 11-1-2004 26.5 12-1-2004 27.5 13-1-2004 28.0 14-1-2004 29.5 15-1-2004 28.0 16-1-2004 28.8
    What do I need to change in the code?
    Thanks,
    perl_seeker :)