in reply to headache with arrays.
# This is an "array of arrays". Each row is one set of ranges, # a pair of Xs and a pair of Ys my @ranges = ([-180, -170, -130, -120], [-180, -170, -110, -105]); # This array is going to store the counts for each row of @ranges # It is initialized to zero for each row my @range_counts = (0) x @ranges; # This reads default input: file(s) on command-line or STDIN while (<>) { # split the line on whitespace my ($x, $y) = split; # Count through the rows in @ranges for my $R (0..$#ranges) { # Compare if ($x >= $ranges[$R][0] and $x < $ranges[$R][1] and $y >= $ranges[$R][2] and $y < $ranges[$R][3]) { # Increment counter ++$range_counts[$R]; } } } # Go through the rows in @ranges again, printing them and the # corresponding count for my $R (0..$#ranges) { printf "(%d..%d, %d..%d): %d\n", @{$ranges[$R]}, $range_counts[$R]; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: headache with arrays.
by Anonymous Monk on Sep 20, 2004 at 16:32 UTC |