From: J�rgen Exner on 9 Mar 2010 13:12 Don Pich <dpich(a)polartel.com> wrote: What does your question have to do with regular expressions (s. Subject)? >I was wondering if there is a way to have perl check for a whole number? perldoc -f int if ($x == int $x) {...} >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. I don't see an equation here, at most an expression. If you evaluate that expression, then int() happens to work for this example, but it won't in all cases because of the binary representation of floating point numbers. But it appears you are not being interested in "x begin a whole number" at all but instead you are interested in "is x dividable by y". And for that question there is the modulo operator %. jue
From: J�rgen Exner on 9 Mar 2010 13:19 Don Pich <dpich(a)polartel.com> wrote: >Probably nothing. But not being 100% efficient with Perl, I wasn't sure. What probably nothing? What weren't you sure about? Please quote sufficient context, as has been a proven custom for over 2 decades, such that your readers can understand what you are writing about. >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? If I were you I wouldn't. Instead I would use http://search.cpan.org/~kraih/Net-Subnets-1.01/lib/Net/Subnets.pm or one of its brethrens. jue
From: J�rgen Exner on 9 Mar 2010 14:34 J�rgen Exner <jurgenex(a)hotmail.com> wrote: >Don Pich <dpich(a)polartel.com> wrote: [...] >>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). > >If I were you I wouldn't. Instead I would use >http://search.cpan.org/~kraih/Net-Subnets-1.01/lib/Net/Subnets.pm or one >of its brethrens. Forgot to mention: And if for whatever reason I were to handroll my own code instead of using a ready-made module then I would use the bitwise boolean operators instead of fumbling around with divisions. jue
From: Peter Makholm on 10 Mar 2010 02:20 Don Pich <dpich(a)polartel.com> writes: > 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? It is not simple, if at all possible, to do with an regexp. What you need is the modulo operator which gives you the remainder from a integer division. if 'x % y' is zero then x/y is an integer. //Makholm
First
|
Prev
|
Pages: 1 2 Prev: Help on String to array ! Next: FAQ 8.36 How do I fork a daemon process? |