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 07-06-2017   -   Original Discussion by LinQ
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 07-06-2017   -   Original Discussion by LinQ
jgt's Unix or Linux Image
jgt jgt is offline Forum Advisor  
Registered User
 
Join Date: Apr 2007
Last Activity: 21 November 2017, 6:58 AM EST
Location: 44.21.48N 80.50.15W
Posts: 2,002
Thanks: 2
Thanked 272 Times in 255 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 (07-06-2017)
Sponsored Links
    #3  
Old Unix and Linux 07-06-2017   -   Original Discussion by LinQ
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 07-14-2017   -   Original Discussion by LinQ
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 04:09 PM
Perl: backslash in front of integer like \32768 LessNux Shell Programming and Scripting 1 01-20-2014 12:55 PM
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 02:08 PM
test the string is char or integer rajesh08 UNIX for Advanced & Expert Users 10 10-10-2008 11:47 AM
C function to test string or integer qqq Programming 3 03-10-2005 01:55 AM



All times are GMT -4. The time now is 06:58 PM.