Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if a string is an integer value.

Status
Not open for further replies.

MonsterAar

Programmer
Aug 16, 2006
24
AU
How do I check that a particular string, being a number, is an integer?

For example, in a program I am wriing i need to do the following two things that involve this:

unless($input is an integer) { blah }

and

if($value /2 is an integer) { blah }

Can anyone help me?

Thankyou,
Luke
 
See if this helps:
Code:
my $num = 5.05;
print "$num is an INT" if $num == int($num);
print "$num is not an INT" if $num != int($num);
 
Thanks rharsh, that works perfectly.

My code now looks like this:

unless($input eq int($input)) {
print "\nYou must enter an integer.\n";
next;
}

and

if($value / 2 eq int($value / 2)) { $value = $value / 2; }
else { $value = ( $value * 3 ) + 1 }

Thanks again,
Luke
 
Sorry to reply again, but I have found that this doesn't work for really large integers.

For example, if I input 99999999999999999999, i get the statement "You must enter an integer.".

I'm using bignum, so I don't know what the problem is.

The highest integer i can put in is 999999999999999, and when I do this, the if statement later in the program doesnt work properly.

This is my program so far. Maybe seeing the whole program will help.

Code:
#!/usr/bin/perl -w
#perl2exe_include Math::BigInt
#perl2exe_include Math::BigInt::Calc

#################################################################
# __________                   __               __              #
# \______   \_______  ____    |__| ____   _____/  |_            #
#  |     ___/\_  __ \/  _ \   |  |/ __ \_/ ___\   __\           #
#  |    |     |  | \(  <_> )  |  \  ___/\  \___|  |             #
#  |____|     |__|   \____/\__|  |\___  >\___  >__|             #
#                         \______|    \/     \/                 #
#   ___ ___        .__.__            __                         #
#  /   |   \_____  |__|  |   _______/  |_  ____   ____   ____   #
# /    ~    \__  \ |  |  |  /  ___/\   __\/  _ \ /    \_/ __ \  #
# \    Y    // __ \|  |  |__\___ \  |  | (  <_> )   |  \  ___/  #
#  \___|_  /(____  /__|____/____  > |__|  \____/|___|  /\___  > #
#        \/      \/             \/                   \/     \/  #
#                                                               #
#################################################################

#hailstone.pl : Checks truth of Hailstone Sequences
#Written by Luke Aaron
#Version B 14/08/06

use bignum;

$loop = 1;
$delq = 0;

while($loop eq 1) {
  print "\nStart run (R) or quit(Q): ";
  $cmd = <STDIN>;
  chomp($cmd);
  $cmd =~ tr/A-Z/a-z/;
  if($cmd eq 'q') { last }
  unless($cmd eq 'r') {
    print "\nYou must enter either R or Q.\n";
    next;
  }
  $loop = 2;
}

while($loop eq 2) {
  print "\nInput starting integer: ";
  $input = <STDIN>;
  chomp($input);
  unless($input eq int($input)) {
    print "\nYou must enter an integer.\n";
    next;
  }
  $loop = 3;
}

while($loop eq 3) {
  $value = $input;
  chdir('C:\\');
  if($delq eq '1') {
    unlink('hailstone.txt');
  }

  $delq = 1;
  $frun = 1;

  while($value ne 1) {
    unless($frun eq 1) {
      if($value / 2 eq int($value / 2)) { $value = $value / 2; }
      else { $value = ( $value * 3 ) + 1 }

      $file = '<hailstone.txt';
      open(FILE, $file);

      #Search file for $value.
      #If found, print  "False for $input\n", stop program, but do not delete file.

      close(FILE);
    }

    $file = '>>hailstone.txt';
    open(FILE, $file);

    print FILE "$value\n";

    close(FILE);

    $frun = 0;
  }

  print "True for $input\n";
  $input = $input + 1;
}

print  "Program terminated.\n"
 
'ne' and 'eq' are string operators, not numerical operators. If you are using numbers use != and == instead of 'ne' and 'eq'.

If you want to make sure only whole numbers are entered use a regexp to check:

Code:
while($loop == 2) {
  print "\nInput starting integer: ";
  $input = <STDIN>;
  chomp($input);
  [b]unless($input =~ /^\d+$/) {[/b]
    print "\nYou must enter an integer.\n";
    next;
  }
  $loop = 3;
}

if it's possible to have negative numbers use:

Code:
unless($input =~ /^-?\d+$/) {



 
Thanks KevinADC, that works for the unless part, but not in the if statement later.

Code:
if($value / 2 =~ /^\d+$/) { $value = $value / 2; }
      else { $value = ( $value * 3 ) + 1 }

is what I have. Is that the correct use of the regexp? if not how do I implement it there? If it is not possible to use it in this way, what is a better way of achieving what I want? ie when the number is even, divide it by two and if it is odd, multiply it by three and then add 1?

Thanks,
Luke
 
use the modulus operator '%' to check for eveness or oddness of numbers:

Code:
if ($value % 2 == 0) { 
   even number
}
else {
   odd number
}

modulus returns the remainder of division, and since perl treats 0 as false you can do something like this:

Code:
if ($value % 2) {
   odd number
}
else {
   even number
}

if your script is only interested in using integers, you may also be interested in the 'integer' pragma:

 
Thanks Kevin,
I forgot about the Modulo divide operator.
My program now works exactly how I'd like it, just one bit of code to work out now!

Thanks all,
Luke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top