From: Don Pich on
I was wondering if there is a way to have perl check for a whole number?

In an equation, if I have 16/4, it will come up with 4 and will set the
"check" to true. If I have 16/5, it will come up with 3.2 and set the
condition false.

Is there a method to get this into an if/else condition?
From: ccc31807 on
On Mar 9, 9:31 am, Don Pich <dp...(a)polartel.com> wrote:
> I was wondering if there is a way to have perl check for a whole number?

Depends on the context as to how you would implement it. If you want
just digits, /[0-9]+/ will match one or more digits. Obviously you
would want to match the particular substring you are looking for,
since this RE would match any line that contained at least one digit.

CC.
From: Ben Morrow on

Quoth Don Pich <dpich(a)polartel.com>:
> I was wondering if there is a way to have perl check for a whole number?

int($x) == $x

or

use POSIX qw/modf/;

(modf $x)[0] == 0;

Ben

From: sln on
On Tue, 09 Mar 2010 08:31:48 -0600, Don Pich <dpich(a)polartel.com> wrote:

>I was wondering if there is a way to have perl check for a whole number?
>
>In an equation, if I have 16/4, it will come up with 4 and will set the
>"check" to true. If I have 16/5, it will come up with 3.2 and set the
>condition false.
>
>Is there a method to get this into an if/else condition?

print "whole 16/4\n" if !(16 % 4);
print "fraction 11/5\n" if 16 % 5;

Whats regex have to do with it?
From: Don Pich on
Probably nothing. But not being 100% efficient with Perl, I wasn't sure.

So let's add another twist to this. The reason I asked this question is
that I have a script that is prompting a user for an IP address. I have
them entering it one octet at a time. The first three octets should be
fine with whatever is place in stin. But the forth octet will need to
follow the subnet rules (i.e., the network address of a /30 would have
the fourth octet as 0, 4, 8, 12, 16 etc. The network address of a /29
would have the fourth octet as 0, 8, 16, 24, 32).

How would I apply the following code so that it will follow subnet rules?

Here is the piece of the code that picks out the octets:

_____CODE______


# Find out the IP address
while ($OCT1_COUNT == 1)
{
print "\nWhat is the First octet? ";
$OCT1 = <STDIN> ;
if (($OCT1 < 0) || ($OCT1 > 255))
{
print "That number is not between 1 and 254.\n";
}
else
{
chomp ($OCT1);
$OCT1_COUNT++;
}
}
while ($OCT2_COUNT == 1)
{
print "\nWhat is the Second octet? ";
$OCT2 = <STDIN> ;
if (($OCT1 < 0) || ($OCT1 > 255))
{
print "That number is not between 1 and 254.\n";
}
else
{
chomp ($OCT2);
$OCT2_COUNT++;
}
}
while ($OCT3_COUNT == 1)
{
print "\nWhat is the Third octet? ";
$OCT3 = <STDIN> ;
if (($OCT1 < 0) || ($OCT1 > 255))
{
print "That number is not between 1 and 254.\n";
}
else
{
chomp ($OCT3);
$OCT3_COUNT++;
}
}
while ($OCT4_COUNT == 1)
{
print "\nWhat is the Fourth octet? ";
$OCT4 = <STDIN> ;
if (($OCT1 < 0) || ($OCT1 > 255))
{
print "That number is not between 1 and 254.\n";
}
else
{
chomp ($OCT4);
$OCT4_COUNT++;
}
}



_____/CODE_____