#!/usr/bin/perl -w $foo = 0; # not a good way to open a file, # should be open FOO, "<$path/$file" # also print after die not needed open (VECTORS, "vectors") or die print "Cannot open 'vectors' file: $!\n"; while ($line = ) { if ($line =~ /\d/) { chomp $line; ($vectorA[$foo], $angleA[$foo], $vectorB[$foo], $angleB[$foo]) = split " ", $line, 4; # debugging print print"file input $foo: $vectorA[$foo], $angleA[$foo], $vectorB[$foo], $angleB[$foo]\n"; ++$foo; } } # this is wrong $foo is 1 as you incremented it # at the end of the loop, so when you hit the last # element you increment $foo 1 too much and set # $max_vector to 1 to0 much $max_vectors = $foo; # debugging print print "\$max_vectors: $max_vectors\n"; for ($foo = 0; $foo <= $max_vectors; ++$foo) { # debugging print print"\$foo $foo\n"; # this should be = for first assignment not += $X[$foo] += ($vectorA[$foo] * cos($angleA[$foo])); $X[$foo] += ($vectorB[$foo] * cos($angleB[$foo])); } # this is another loop just like the one above # why not put the two statements here with the two above? # and just have the one loop # this is also the longhand++ loop syntax for ($foo = 0; $foo <= $max_vectors; ++$foo) { # debugging print print"\$foo $foo\n"; # this should be = for first assignment not += $Y[$foo] += ($vectorA[$foo] * sin($angleA[$foo])); $Y[$foo] += ($vectorB[$foo] * sin($angleB[$foo])); } # Must do more stuff here, ie. finish and convert to degrees after sin() and cos() foreach $X (@X) { print "$X\n"; } print "\nblah\n\n"; foreach $Y (@Y) { print "$Y\n"; } #!/usr/bin/perl -w use strict; my (@vectorA, @angleA, @vectorB, @angleB, @X, @Y); open (VECTORS, ") { next unless $line =~ /\d/; $i++; ($vectorA[$i], $angleA[$i], $vectorB[$i], $angleB[$i]) = split/\s+/,$line; } die "Sorry, no data!\n" if $i == -1; my $max_vectors = $i; for (0..$max_vectors) { $X[$_] = ($vectorA[$_] * cos($angleA[$_])); $X[$_] += ($vectorB[$_] * cos($angleB[$_])); $Y[$_] = ($vectorA[$_] * sin($angleA[$_])); $Y[$_] += ($vectorB[$_] * sin($angleB[$_])); } # Must do more stuff here, ie. finish and convert to degrees after sin() and cos() print "$_\n" for @X; print "\nblah\n\n"; print "$_\n" for @Y;