Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Help with a programming problem.

by metehan92 (Initiate)
on Sep 05, 2012 at 23:07 UTC ( #991967=perlquestion: print w/ replies, xml ) Need Help??
metehan92 has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys, I am trying to answer a code-chef problem, using perl. To be specific I am trying to answer this problem. http://www.codechef.com/SEP12/problems/HORSES I am running to an issue however, around lines 6-10, my program seems to freeze after I enter the following (just an example)
1 2 3 4
It's strange, after the last input taken in, the program just freezes, CPU usage for the process jumps up to 14% and it just sits there, I've tried putting in print statements to check my progress, but it doesn't help. Any ideas?
#!/usr/bin/perl $i1 = <>; chomp($i1); for ($count = 0; $count < $i1; $count++) { $i2 = <>; $i3 = <>; chomp $i2; chomp $i3; my @values; @values = split(/ /, $i3); my @differences; my $count1; my $count2; my $min; for ($count1 = 0; $count1 < $i2; $count++) { for ($count2 = $count1+1; $count2<$i2; $count2++) { if($count1 = 0 and $count2 = 1) { $min =abs($values[$count1]-$values[$count2]); } elsif($min> abs($values[$count1]-$values[$count2])) { $min = abs($values[$count1]-$values[$count2]); } } } print $min; }

Comment on Help with a programming problem.
Select or Download Code
Re: Help with a programming problem.
by Anonymous Monk on Sep 06, 2012 at 00:10 UTC
    You can find errors by adding
    use strict; use warnings;
    at the top of the program.
Re: Help with a programming problem.
by philiprbrenan (Monk) on Sep 06, 2012 at 00:44 UTC

    Which as the anonymous monk so rightly implies would show you that $count has not been declared in:

    for ($count1 = 0; $count1 < $i2; $count++)

    and so the loop never terminates.

Re: Help with a programming problem.
by Anonymous Monk on Sep 06, 2012 at 00:46 UTC
    I didn't mean to give such a short answer. You are just learning the language. One problem was in your line for ($count1 = 0; $count1 < $i2; $count++) . That should $count1++. You dropped the '1' at the end of the name. This is an example of where, by enabling strict and warnings, perl would have caught the error and reported it to you.

    This requires you to declare all the variables in the program. Some of the variables are declared and some are not. You need to declare all of them. Like in the line with the error, you could declare the loop variable, my $count1

    for (my $count1 = 0; $count1 < $i2; $count1++)

Re: Help with a programming problem.
by GrandFather (Cardinal) on Sep 06, 2012 at 02:30 UTC

    Your for loops are much better written:

    for my $count (0 .. $i1 - 1) {

    which both avoids the immediate problem and makes the action of the loop much clearer (assuming that you recognise the range operator '..', or at least can infer what it does).

    True laziness is hard work
Re: Help with a programming problem.
by grizzley (Chaplain) on Sep 06, 2012 at 06:57 UTC
    The problem is in line if($count1 = 0 and $count2 = 1) You assign 0 and 1 to values $count1 and $count2 instead of comparing them. Use == as comparison operator.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991967]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-07-22 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (107 votes), past polls