Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I've just started to learning perl and decided to write up a script to add vectors for my silly Physics class. I've gotten maybe halfway through it, and I am confounded by errors.
My program opens a file 'vectors' which contains 4 numbers, which are then assigned to variables using split(); The errors I am getting involve the use of unitialized variables so I think my use of split() is to blame.
Here's the script:
Here's the 'vectors' file I refer to:#!/usr/bin/perl -w $foo = 0; 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]) = s +plit " ", $line, 4; ++$foo; } } $max_vectors = $foo; for ($foo = 0; $foo <= $max_vectors; ++$foo) { $X[$foo] += ($vectorA[$foo] * cos($angleA[$foo])); $X[$foo] += ($vectorB[$foo] * cos($angleB[$foo])); } for ($foo = 0; $foo <= $max_vectors; ++$foo) { $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"; }
Thanks for any help.Vector Addition File 9.8 0 10.878 115
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbie Errors
by tachyon (Chancellor) on May 30, 2001 at 08:14 UTC | |
|
Re: Newbie Errors
by Sherlock (Deacon) on May 30, 2001 at 07:28 UTC | |
|
Re: Newbie Errors
by chipmunk (Parson) on May 30, 2001 at 07:32 UTC | |
|
Re: Newbie Errors
by Desdinova (Friar) on May 30, 2001 at 09:01 UTC | |
|
Re: Newbie Errors
by sierrathedog04 (Hermit) on May 30, 2001 at 14:29 UTC | |
|
Re: Newbie Errors
by cacharbe (Curate) on May 30, 2001 at 07:02 UTC | |
by Anonymous Monk on May 30, 2001 at 07:22 UTC | |
by Sherlock (Deacon) on May 30, 2001 at 07:34 UTC |