Perl Script Integer Test

Working out a small problem, I have a need of a Perl snippet which might look something like this:

use integer;

...

if ($changingNumber / 2)
{
    do something;
}

else
{
    do something else;
}

...

What I want to happen is for "if" to resolve as "true" every time a whole number is produced by the quotient of "$changingNumber / 2"; allowing the "do something" block only then to be executed.

But what I get, instead, is the eval of "true" and the consequent "do something" block execution for every value of "$changingNumber" of 2 and greater.

I know this is probably a simple error on my part, but I can't quite elucidate what would do the trick here...

Any ideas?

Thanks!

use the mod function. if mod (n,2) =0 then n is an even number.

1 Like

Thanks, jgt; I'll dig in and see what comes about :wink:

Just a quick check-back to pass along the syntax I finally settled on:

if ($var % 2 == 0)
{

       do something;

}
...

This was a tidy, basic implementation of the mod (%) function in my use case; and might be helpful for someone else who happens this way ;o)

Cheers --