Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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. 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; }

Replies are listed 'Best First'.
Re: Help with a programming problem.
by GrandFather (Sage) 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 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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991967]
Approved by davido
[choroba]: that was around 1994 or 95
hippo actually introduced Linux to his university.
Discipulus mmh will be a fun meditation: How I started with computers and programming..
[Discipulus]: bravo hippo! well done

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2018-06-22 09:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (123 votes). Check out past polls.