Debugging?
Andrew_Levenson
created: 2006-03-03 12:30:49
I just started learning Perl; this is the first program i've ever tried to write. I can't really figure out what i'm doing wrong (one, because I don't know, and two, because my interpreter won't stay open long enough to tell me; it closes right when the program finishes). Can anyone help me figure out what i'm doing wrong? This is supposed to find primes between two numbers. (And if you could help with the interpreter issue, that'd be nice, too).
  	
	use strict;
	use warnings;
	#finds prime numbers between two points;

print "Please enter the values for the 2 numbers between which you wish to 

find the primes.\n";
$x=; chomp($x); print "\n"; 	#user inputs point x;	
$y=; chomp($y); print "\n";	#user inputs point y;
for($i==$x; $i==$y; $i++){
	$even=0;
	$notprime=0;
	$j=$i/2;
	if(int($j)==$j){
		$even=1;		# tests to see if it is even;
	}	
	else{
		$n=$i**(1/2);
		for($m=3; $m=$n; $m+=2){
			$o=$i/$m;
				if(int($o)==$o){
					$notprime=1;
	}
		}	
			}	

	if($even==0 && $prime==0){
		print "$i is prime!\n";

		}
	}
		

$q=; # stops the program from closing...;
If anyone wants to help me figure out what's wrong... just let me know what you did? Thanks. Thanks!
Re: Debugging?
created: 2006-03-03 12:37:53

You say "my interpreter won't stay open long enough to tell me; it closes right when the program finishes"

I guess you are running the program by double clicking the .pl file. Try opening a command (cmd) window and running the program there.

See also perldoc perldebug

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Re: Debugging?
created: 2006-03-03 12:39:39
Your use strict line is causing all sorts of helpful error messages to spew forth, but you are not seeing the error messages, because the $q= trick doesn't work with compile time errors. First, open a command line window and run the program from there, don't just click on the file. Then check out node 111088 and the links from it, and the strict documentation on how to fix those errors. Mostly you will be putting my (see that also) in front of the first occurance of every variable.
Re: Debugging?
created: 2006-03-03 12:41:06

Just for starters,

  • You should really put
    use strict;
    use warnings;
    at the top of your program. This will save you lots of grief, even though you'll have to start declaring your variables.

  • You still have the for wrong. I suggest you re-read the documentation.
    for($i==$x; $i==$y; $i++)
    should be
    for(my $i=$x; $i==$y; $i++)
    (my added as a bonus)
    Or if you want to include $y,
    for(my $i=$x; $i<$y; $i++)

    But why not use the easier to read/understand/write
    for my $i ($x..$y-1)
    Or if you want to include $y,
    for my $i ($x..$y)

    By the way, you're assuming (i.e you don't validate) that the user supplies integers for $x and $y, and that the number he supplied for $x is smaller than or equal to $y.

Re: Debugging?
created: 2006-03-03 12:46:06
I'm on my way out so I will look at your code later. The first thing you want to know about is the perl debugger. It is activated by:
/usr/bin/perl -d