Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Modulus zero error

by Anonymous Monk
on Dec 24, 2004 at 04:51 UTC ( #417251=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Perl keeps telling me "Use of uninitialized value in modulus (%)" in this line where I have an if statement (and no modulus). If I change that line to remove the part after the &, it works fine... both of the variables are defined if I try to print them somewhere near there.
for($x = $prevmax;$x<=$max;$x++) { $n = sqrt($x); $i = 0; while ($x%$p[$i] !=0) { $i++; if (($n < $p[$i]) & ($x > $p[-1])) # error here { print (1 & ($x > $p[-1])); push(@p,$x."\n"); last; } } }
Could anyone help me? Thanks

Considered by xdg: "retitle 'uninitialized value in modulus'"
Unconsidered by davido: Final vote of 11/26/0 indicated significant opposition to retitling.

Replies are listed 'Best First'.
Re: Modulus zero error
by BrowserUk (Patriarch) on Dec 24, 2004 at 05:13 UTC

    Your looking at the wrong line (or being pointed at th wrong one?). The error is in this line:

    while ($x % $p[$i] !=0) __________^

    Examine what is said, not who speaks.        The end of an era!
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Yeah... I didn't really understand the line number - it said "line 25, <> line 3." (line 25 is the if statement) And when I truncate line 25, the error stops happening - i don't see how that line can affect the while part, since it's not changing anything..

        The answer is perl has built a hash table in your while statement but when it comes to if it see's $p.. which is a new variable accordint to perl.. So there comes the problem. This is why it states the error in a wrong place

        Use Strict and warnings

        OOps sorry i mis understood the problem. Ok The problem lies in this line
        if (($n < $p[$i]) && ($x > $p[-1])) # error here

        just remove the previous code and use this u will not see the warning.
        if (($n < $p[$i]))

        This is becasue the lt operator should work by finding the modulus and then determine the solution.(I am not sure with that but for this error i hope this could be the reason. i will update on you again after 15 minutes

        Sorry i was not able to find it out....

        Sasi Kumar
Re: Modulus zero error
by hv (Prior) on Dec 24, 2004 at 12:34 UTC

    Some version of perl had problems reporting the correct line number for errors, and it is likely in this case that it is reporting the correct error with the wrong line number.

    I'd suggest adding diagnostic warn() statements throughout the program to pinpoint where it reached just before the error message, or alternatively concentrate on places in the program that the % character appears.

    If you are not using % as a modulus operator in your program, it is possible that mismatched quotes or brackets are causing a %$hashref to be misinterpreted.


Re: Modulus zero error
by xdg (Monsignor) on Dec 24, 2004 at 13:42 UTC

    I don't think we can give a solid answer without knowing better what @p contains. If @p is empty or is a new variable ("use strict"), then the first time through the loop while ( $x % $p[$i] != 0 ) could report a "use of undefined in modulus" because $p[$i] is undefined.


    Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Re: Modulus zero error
by Anonymous Monk on Dec 24, 2004 at 21:36 UTC
    oops... sorry, I think the whole thing was because I'd used the variable $i somewhere else, which screwed everything up

Re: Modulus zero error
by jalewis2 (Monk) on Dec 25, 2004 at 02:45 UTC
    I was just getting this error yesterday and it was making me crazy. I was doing this at the start of my script.

    my $x = undef;

    Not realizing that the error was telling me $x needs to be something. I changed it from undef to 0 and that fixed it. Thinking back, trying to do the operation on an undefined variable was dumb, but I didn't "see" it.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://417251]
Approved by BrowserUk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-09-29 01:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found