use strict; use warnings; # important, too. #-- variables and handles my @Matrix = map { [ (0) x 361 ] } 0..360; #--Open the input and output files # use 3-arg open, use lexical filehandle, don't interpolate a variable # that doesn't need anything added to it, i.e., "$inf" is bad, but "$inf-foo" # is not, report WHY open fails, not merely that it has failed. open my $in, '<', $foo or die "Can't read $inf: $!"; # lexical $line with minimal scope, check for definedness # (if you have a line that is "0\n", you'll find your original code # will terminate early). while (defined (my $line = )) { # usually, split ' ', $line is what you want instead of /\s+/. # but they do slightly different things, so I'm not changing it. # Added "my" so we create a new @result each time. my @result = split(/\s+/, $line); # I'm guessing here since I don't know where $phi and $psi really # come from. my $phi = int($result[0]); my $psi = int($result[1]); # use $ instead of @ to signify we want a scalar value from # the array. Use ++ instead of += 1 because it's more idiomatic # though technically it's no big deal either way. $Matrix[$phi+180][$psi+180] ++; }