From: Don Pich on 9 Mar 2010 09:31 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 9 Mar 2010 09:45 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 9 Mar 2010 09:58 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 9 Mar 2010 10:19 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 9 Mar 2010 10:54 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_____
|
Next
|
Last
Pages: 1 2 Prev: Help on String to array ! Next: FAQ 8.36 How do I fork a daemon process? |