假设我有一个函数“start_ip_address”,“ip_address_increment”和“number_of_increment”的参数。
我想打印start_ip_address,然后该地址递增增量number_of_increment次。
因此,如果“start_ip_address”= 192.168.0.0且“ip_address_increment”= 0.0.1.1且“number_of_increment”= 3,我想打印:
192.168.0.0
192.168.1.1
192.168.2.2
192.168.3.3
我该怎么做?我对Perl字符串操作非常糟糕。
答案 0 :(得分:3)
IPv4地址只是32位整数。每个数字代表一个字节。
sub to_num { unpack 'N', pack 'C4', split /\./, $_[0] }
sub fr_num { join '.', unpack 'C4', pack 'N', $_[0] }
my $ip = to_num($start_ip_address);
my $inc = to_num($ip_address_increment);
say fr_num($ip);
for (1..$number_of_increments) {
$ip += $inc;
say fr_num($ip);
}