I've written some code that (I think) does what you want. Being new to Perl, it will probably take you some study before you understand the nested structures (array of arrays, which are done with references). I recommend
perldoc perlreftut.
# 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];
}
Caution: Contents may have been coded under pressure.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.