Here is your code with some comments on errors, etc.

I have added some debug prints so you can see what gives. This is alway a good start to debugging.

First you are not opening the file "correctly" with a path and an I/O operator like < for read and > or >> for write.

The uninitialised values arise because you increment $foo to a value one past the last element. In the exapmle the while fails after the first iteration. We have an array of length 1 ie only element '0' but $foo = 1. You use this value as a count of the number of elements and iterate from 0..1, thus using an uninit value.

I could not resist cleaning up your code. See the final version. Of all the changes the split/\s+/,$line is one of the more important as now your data file can be "dirty" with multiple spaces between values and still work. Single space delimiters are fine provided no human hands ever touch the data file!

hope this helps

tachyon

#!/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 = <VECTORS>) { 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, "<c:/vectors.txt") or die "Oops $!\n"; my $i = -1; while (my $line = <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;

In reply to Re: Newbie Errors by tachyon
in thread Newbie Errors - Uninitialized Values by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.