in reply to odd or even day?

The following works:
#!/usr/bin/perl -w use strict; my $var=time/(24*60*60); print $var%2 ? "odd\n" : "even\n";

The only difference is the parentheses. I'm not sure why they are required. Perhaps somebody else can explain that...

Replies are listed 'Best First'.
Re^2: odd or even day?
by GrandFather (Saint) on Jan 07, 2006 at 18:12 UTC

    * and / have the same precedence and evaluate left to right so time/24*60*60 is equivelent to (time/24)*60*60, which is not what OP intended.

    Update: See perlop Operator Precedence and Associativity.


    DWIM is Perl's answer to Gödel
Re^2: odd or even day?
by demerphq (Chancellor) on Jan 07, 2006 at 18:13 UTC

    The only difference is the parentheses. I'm not sure why they are required. Perhaps somebody else can explain that...

    I think its because perl follows standard mathematical operator precedence rules: B.E.D.M.A.S (Brackets, exponents, division, multiplication, addition, subtraction.)

    update: Grandfather's explanation is better.

    ---
    $world=~s/war/peace/g

      Thanks for the replies. I'd completely forgotten about precedence rules, seems to have sorted it out.
      Thanks alot
      Jon