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

Hi Monks, I have a simple question. I have a variable say 'day' This variable is increasing daily by one and I need the following logic: If day = 3 or 6 or 9 or 12 etc.. then execute some logic Can I just use a mod for this if (day%3) = 0 do something Thanks

Replies are listed 'Best First'.
Re: modulus question
by GrandFather (Saint) on Feb 03, 2015 at 05:48 UTC

    What happened when you tried it?

    If you are having trouble with the code to implement the test let us see what you've tried and we'll help sort it out for you (the "code" you gave won't work in Perl due to syntax issues).

    Perl is the programming world's equivalent of English
Re: modulus question
by vinoth.ree (Monsignor) on Feb 03, 2015 at 05:50 UTC

    To check if a number $day is divisible by divisor(3), use the modulus operator, %:

    If the number divisible by 3 then the modulus will return remainder 0, so the if condition will fail so add your logic in else condition.

    if ($day % 3) { # does not divide cleanly } else { # does. }
    (OR)

    Even you can use the same as your if(($day%3) == 0) condition of checking the reminder is equal to 0. In this case you can add your logic in if(..) itself


    All is well

      vinoth in a case like this by not providing any code there is a chance that the OP will show us what he has tried and thus allow us to provide much better help than we can by just giving a dollop of code.

      Very often the errors beginners make fall into common patterns and tell you a lot about how they are thinking and where the root of their problem really lie. By just giving a solution, even if we take time to explain how the solution works, beginners will run off happy, but none the wiser.

      Perl is the programming world's equivalent of English
        Hi GrandFather,

        Yes I agree with you, I regret for that. As per my understanding I though he just confused if(....) condition with modulus(%) operator. That's why I just given him the explanation.


        All is well
A reply falls below the community's threshold of quality. You may see it by logging in.