in reply to Variable initialization / reinitialization
This reminds me a bit of what finally pushed me over the edge from bash to Perl many years ago - validating and sorting lists of IP addresses. In hindsight, I bet there's a module for the task, but it was a good exercise. Anyhow, borrowing from that years-old effort:
use strict; use warnings; while(<DATA>){ chomp; my $mask = (split(/\s+/, $_))[2]; print "Mask $mask is ", &Valid($mask), "\n"; } sub Valid { my @octs; local $_ = shift; return 0 if !defined $_; return 0 if m{(?:(?:^|\.)[^0]\d*\.)(?!255)}; return 0 if (@octs = split(/\./, $_)) ne 4; return 0 if grep {!m/^(?:0|3|7|15|31|63|127|255)$/} @octs; return 1; } __DATA__ field1 10.1.253.11 0.0.0.0 field1 10.1.254.0 0.0.0.64 field1 10.1.254.128 0.0.0.63 field1 10.1.158.0 15.255.0.255 field1 10.1.160.0 0.0.0.37 field1 10.1.161.0 0.0.146.255 field1 10.1.161.0 0.0.255 field1 10.1.161.0 0.0.255.255 field1 10.1.161.0 255.0.0.0 field1 10.1.158.0 15.255.255.127
I'm not sure I followed your non-zero/non-255 rule. I interpreted it as, once there's a non-zero octet, all thereafter must be 255.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Variable initialization / reinitialization
by BTrey (Novice) on Feb 09, 2009 at 21:52 UTC |