Perl, using recursion. Tests included. It throws an exception if the end address precedes the start one.
#!/usr/bin/perlusewarnings;usestrict;sub _ips_between{my($list1,$list2,$sum)=@_;return$sumunless@$list1;my($e1,$e2)=(shift@$list1,shift@$list2);die"End before start.\n"if$e2<$e1&&$sum==0;return_ips_between($list1,$list2,$sum+($e2-$e1)*256**@$list1)}sub ips_between{_ips_between([split/\./,shift],[split/\./,shift],0)}useTest::Moretests=>6;useTest::Exception;isips_between('10.0.0.0','10.0.0.50'),50;isips_between('10.0.0.0','10.0.1.0'),256;isips_between('20.0.0.10','20.0.1.0'),246;isips_between('0.255.127.12','1.2.3.4'),164_856;isips_between('1.1.1.1','1.1.1.1'),0;throws_ok{ips_between('1.1.1.1','1.1.0.1')}qr/End before start/;
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Perl, using recursion. Tests included. It throws an exception if the end address precedes the start one.