in reply to inefficient code? works on small files ok but not larger ones

Given the simplicity and regularity of your ranges, you could simply compute indices of a two-dimensional array instead, like this:
use strict; use POSIX qw(floor); my $input = $ARGV[0]; open(FILE, $input) || die "ERROR: Unable to open $input FILE for readi +ng: $!\n"; my @range2; my ($phi, $psi); while (<FILE>) { $phi = substr($_, 0, 6); $psi = substr($_, 8, 6); print "$phi $psi\n"; my $i = floor($phi/5.0) + 36; my $j = floor($psi/5.0) + 36; if ($i >= 0 && $j >= 0 && $i < 73 && $j < 73) { ++$range2[$i][$j]; } } for my $i (0..72) { for my $j (0..72) { my $val = $range2[$i][$j]; $val ||= 0; print "$val, "; } print "\n"; }

Replies are listed 'Best First'.
Re^2: inefficient code? works on small files ok but not larger ones
by graff (Chancellor) on Oct 06, 2004 at 02:42 UTC
    I agree -- this sort of approach makes a lot more sense, and will scale very well to any amount of input. I'd just make one minor adjustment:
    while (<FILE>) { # $phi = substr($_, 0, 6); # $psi = substr($_, 8, 6); ( $phi, $psi ) = split; ...
    The OP didn't show us any lines of data with single-digit numbers in one or both columns. Such lines might or might not have fixed-width fields, and if not, substr() would do the wrong thing; split() would always do the right thing.

    (For that matter, it's not clear whether the column separator is two spaces or a single tab character -- again, split works in any case, while substr would get tripped up when you guess wrong about that.)