Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Newbie Errors

by tachyon (Chancellor)
on May 30, 2001 at 08:14 UTC ( [id://84167]=note: print w/replies, xml ) Need Help??


in reply to Newbie Errors - Uninitialized Values

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://84167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-20 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found