Your program compare the last 8 bits; if I want to compare the last 10 bits, how to do ?
Thanl you
Main Topics
Browse All TopicsPlease,
I would want to compare 2 adresses in the same lan, for examples
$ip1 = "172.18.23.9";
$ip2 = "172.18.23.10";
we see here that $ip2 > $ip1, because 10 is greater than 9.
how to write a program in Perl which can make me to
take the last 10 bits(or let say x last bits in general ) and compare them ?
Thank you
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's an example.
$ip1 = '172.18.23.9';
$ip2 = '172.18.22.10';
@ip1_octets = split /\./, $ip1;
@ip2_octets = split /\./, $ip2;
$bits = 10;
$ip1_bin = unpack("b$bits", ($ip1_octets[2] . $ip1_octets[3]));
$ip2_bin = unpack("b$bits", ($ip2_octets[2] . $ip2_octets[3]));
print "ip1_bin: $ip1_bin\n";
print "ip2_bin: $ip2_bin\n";
if ($ip1_bin > $ip2_bin) {
print "$ip1 is greater than $ip2";
}
else {
print "$ip2 is greater than $ip1";
}
$x = 10;
$ip1 = "172.18.23.9";
$ip2 = "172.18.23.10";
$IP1 = unpack'N',pack'C*',split/\
$IP2 = unpack'N',pack'C*',split/\
$IP1 &= (1<<$x)-1;
$IP2 &= (1<<$x)-1;
if( $IP1 < $IP2 ){
print "$ip1:$x < $ip2:$x\n";
}elsif( $IP2< $IP1 ){
print "$ip2:$x < $ip1:$x\n";
}else{
print "$ip1:$x == $ip2:$x\n";
}
There's useful modules for doing IP comparisons as they can get very tricky.
#!/usr/bin/perl
use Net::IPv4Addr;
$ip1 = "172.18.23.9";
$ip2 = "172.18.23.10";
netmask="255.255.255.0"; # Adjust as required, or use CIDR notation
print "$ip1 is in the same network as $ip2\n" if ipv4_in_network($ip1,$netm
Business Accounts
Answer for Membership
by: HamdyHassanPosted on 2004-02-18 at 07:30:26ID: 10393299
Repeated question, please delete one of them
",$ip1); ",$ip2);
$ cat ip_parse.pl
#!/export/vol/bin/perl
$ip1 = "172.18.23.9";
$ip2 = "172.18.23.10";
$ip1 =~ s/\./:/g ;
$ip2 =~ s/\./:/g ;
($x1,$x2,$x3,$x4)=split(":
($y1,$y2,$y3,$y4)=split(":
print $x4." compare to ".$y4."\n";
if ( $y4 > $x4 ) { print $ip1." less than ".$ip2."\n"; } else { print $ip1." greater than ".$ip2."\n"; }
$ ip_parse.pl
9 compare to 10
172:18:23:9 less than 172:18:23:10