in reply to Re^2: Using map to populate array in for loop
in thread Using map to populate array in for loop
This may be more than what you need, but if you use a hash, you can get just the IPs within a specific range:
use strict; use warnings; use Data::Dumper; use 5.010; use Socket 'inet_aton'; my %ips; my @ranges = qw( 192.168.1.10-192.168.1.20 192.168.1.30-192.168.1.40 ); for my $range ( @ranges ) { my ( $start, $end ) = split /-/, $range; my $range = "$start - $end"; @{ $ips{$range} } = map { sprintf "%vi", pack "N", $_ } unpack("N" +,inet_aton($start)) .. unpack("N",inet_aton($end)); } print Dumper \%ips; for my $range (keys %ips){ print "Range: $range\n"; print "\t$_\n" for @{ $ips{$range} }; }
Output:
# dumper output $VAR1 = { '192.168.1.30 - 192.168.1.40' => [ '192.168.1.30', '192.168.1.31', '192.168.1.32', '192.168.1.33', '192.168.1.34', '192.168.1.35', '192.168.1.36', '192.168.1.37', '192.168.1.38', '192.168.1.39', '192.168.1.40' ], '192.168.1.10 - 192.168.1.20' => [ '192.168.1.10', '192.168.1.11', '192.168.1.12', '192.168.1.13', '192.168.1.14', '192.168.1.15', '192.168.1.16', '192.168.1.17', '192.168.1.18', '192.168.1.19', '192.168.1.20' ] }; # for loop output Range: 192.168.1.30 - 192.168.1.40 192.168.1.30 192.168.1.31 192.168.1.32 192.168.1.33 192.168.1.34 192.168.1.35 192.168.1.36 192.168.1.37 192.168.1.38 192.168.1.39 192.168.1.40 Range: 192.168.1.10 - 192.168.1.20 192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 192.168.1.15 192.168.1.16 192.168.1.17 192.168.1.18 192.168.1.19 192.168.1.20
|
|---|