Now let's do this in Perl:
Let's stop for a moment. There are many ways to do the check, here are some of them:# First make the program know the two numbers: $Number1 = 48; $Number2 = 12; # Now divide the numbers: $Result = $Number1 / $Number2; # ...and look if we got a decimal:
I prefer the last one. It compares the number to the integer part (e.g. everything before the . without rounding) and if they're the same - everything is fine.# Using your sub is_integer_string($Result) # Look for a . $Result =~ /\./; # Check if the number is the same as its integer part: $Result == int($Result)
if (condition) { print "Yes"; } else { print "No"; }
Regarding your subroutine... I think it won't work as you expect: Leaving the \s* alone, you're matching a + sign followed by zero or one - sign followed by numbers. +3 matches, but the string representation of a number doesn't include a leading + in Perl. Better to put [] around the \+ and \- (and keep the ?). This will match either a -, a + or nothing before the number.
Perl could do a good part of the job for you if you want to match using a regexp:
Any mathematical operation will see the input variable as a number (including everything Perl accepts here). Adding 0 means: Don't change my number at all, but force it to be a Perl number with a known syntax which could be matched easily.sub is_integer_string { my $N = $_[0] + 0; return $N =~ /^\-?\d+$/; }
Update: Just noticed JavaFan's comment and he's right: modulo is the quickest way to do it, but all the others shown here also work.
In reply to Re: finding multiples of two values
by Sewi
in thread finding multiples of two values
by urnumdei
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |