Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Perl Script Integer Test

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 2 Weeks Ago
LinQ LinQ is offline
Registered User
 
Join Date: Feb 2014
Last Activity: 14 July 2017, 8:43 AM EDT
Posts: 113
Thanks: 63
Thanked 2 Times in 2 Posts
Perl Script Integer Test

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

Code:
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!
Sponsored Links
    #2  
Old Unix and Linux 2 Weeks Ago
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 22 July 2017, 3:32 PM EDT
Location: 44.21.48N 80.50.15W
Posts: 1,950
Thanks: 2
Thanked 255 Times in 241 Posts
use the mod function. if mod (n,2) =0 then n is an even number.
The Following User Says Thank You to jgt For This Useful Post:
LinQ (2 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 2 Weeks Ago
LinQ LinQ is offline
Registered User
 
Join Date: Feb 2014
Last Activity: 14 July 2017, 8:43 AM EDT
Posts: 113
Thanks: 63
Thanked 2 Times in 2 Posts
Thanks, jgt; I'll dig in and see what comes about Linux
    #4  
Old Unix and Linux 1 Week Ago
LinQ LinQ is offline
Registered User
 
Join Date: Feb 2014
Last Activity: 14 July 2017, 8:43 AM EDT
Posts: 113
Thanks: 63
Thanked 2 Times in 2 Posts
Just a quick check-back to pass along the syntax I finally settled on:


Code:
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 --
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl inserting random negative integer selvankj Shell Programming and Scripting 4 09-25-2015 03:09 PM
Perl: backslash in front of integer like \32768 LessNux Shell Programming and Scripting 1 01-20-2014 11:55 AM
Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d Max89 Shell Programming and Scripting 1 11-25-2009 01:08 PM
test the string is char or integer rajesh08 UNIX for Advanced & Expert Users 10 10-10-2008 10:47 AM
C function to test string or integer qqq Programming 3 03-10-2005 12:55 AM



All times are GMT -4. The time now is 02:31 AM.