use Socket qw( inet_aton ); if (inet_aton('83.1.1.1') lt inet_aton('82.1.1.1')) { print("is less than\n"); } else { print("isn't less than\n"); }
All the string comparison operators (lt, gt, le, ge, eq, ne and cmp) work here.
This "trick" is also useful for sorting.
my @sorted_ips = map substr($_, 4), sort map inet_aton($_).$_, @ips;
You might also find the modules Net::IP or NetAddr::IP of interest.
By the way, the problem is your code is that you pad the elements of @calc, yet you use $*_start and $*_end (not the padded elements) to form $calc1 and $calc2.
Update: Added the sorting code and the list of relevant modules.
Update: Changed substr($_, 5) to substr($_, 4).
Update: Added the explanation as to why the OP's code didn't work.
In reply to Re: comparing two ip addresses.
by ikegami
in thread comparing two ip addresses.
by jonsmith1982
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |