Perl for loop
Hi,
I am quiet new to perl. I want to achieve the following result. Find all the Pythagorean triples (a2 +b2=c2) where a,b,c are less than 1000. (a squared + b squared = c squared) To achieve this goal. I created the following code. #!/usr/bin/perl -w for ($a = 1 ; $a < 1001 ; $a++) { for ($bi = 1 ; $b < 1001 ; $b++) { for ($b = 1 ; $b < 1001 ; $b++) { if ((($a * $a) + ($b * $b)) == ($c * $c)) { print " value of (a) = $a \n"; print "value of (b) = $b \n"; print "value of (c) = $c \n"; } } } } When I execute this code, I get the following message. …............ Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. Use of uninitialized value in multiplication (*) at ./problem9 line 10. …........................ Can anyone please help me with this? Thank you in advance. K.Suthagar |
I also tried.
#!/usr/bin/perl -w my $a,$b,$c; for ($a = 1 ; $a < 1001 ; $a++) { for ($bi = 1 ; $b < 1001 ; $b++) { for ($b = 1 ; $b < 1001 ; $b++) { if ((($a * $a) + ($b * $b)) == ($c * $c)) { print " value of (a) = $a \n"; print "value of (b) = $b \n"; print "value of (c) = $c \n"; } } } } |
Hi,
You have a typo: for ($bi = 1 ; $b < 1001 ; $b++) And there is no $c. Shouldn't the second for ($b = 1 ; $b < 1001 ; $b++) be for ($c = 1 ; $c < 1001 ; $c++) Hope this helps. Code:
#!/usr/bin/perl -w |
Hi druuna,
Thank you for the help. I feel like a total idiot now. Sorry for wasting your time. Thank You K.Suthagar |
Hi,
Don't worry about it, stuff like that happens ;) BTW: You're welcome :) |
Code:
#!/usr/bin/env ruby -w |
Highly recommend you add
use strict; as the first line of ALL your Perl programs. You'll thank me later, I promise :) Do continue to use the -w switch as well, they catch different kinds of errors. ie Code:
#!/usr/bin/perl -w http://perldoc.perl.org/ http://www.perlmonks.org/?node=Tutorials |
All times are GMT -5. The time now is 01:02 PM. |