stroke has asked for the wisdom of the Perl Monks concerning the following question:

Trying to get a list of IP addresses from some defined IP ranges. Having found "a line of code" to enumerate each IP in the range and populate an array, I've found this doesn't work as I'd hope - only the IPs from the last range are in the array.

Problem is that I don't really understand exactly what the inet_aton line is doing well enough to work out how to fix it - the peril of copying code, I guess!

#!/usr/bin/env perl 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; say "Found RANGE: $start - $end"; # Get each IP in the range @ips = map { sprintf "%vi", pack "N", $_ } unpack("N",inet_aton($s +tart)) .. unpack("N",inet_aton($end)); #print Dumper(@ips); # prints all IPs } print Dumper(@ips); # prints only last range of IPs # Do stuff with each IP ...

Output from Dumper inside the for loop looks good:

Found RANGE: 192.168.1.10 - 192.168.1.20 $VAR1 = '192.168.1.10'; $VAR2 = '192.168.1.11'; $VAR3 = '192.168.1.12'; $VAR4 = '192.168.1.13'; $VAR5 = '192.168.1.14'; $VAR6 = '192.168.1.15'; $VAR7 = '192.168.1.16'; $VAR8 = '192.168.1.17'; $VAR9 = '192.168.1.18'; $VAR10 = '192.168.1.19'; $VAR11 = '192.168.1.20'; Found RANGE: 192.168.1.30 - 192.168.1.40 $VAR1 = '192.168.1.30'; $VAR2 = '192.168.1.31'; $VAR3 = '192.168.1.32'; $VAR4 = '192.168.1.33'; $VAR5 = '192.168.1.34'; $VAR6 = '192.168.1.35'; $VAR7 = '192.168.1.36'; $VAR8 = '192.168.1.37'; $VAR9 = '192.168.1.38'; $VAR10 = '192.168.1.39'; $VAR11 = '192.168.1.40';

Outside, not so - the array only has the last range IPs

Found RANGE: 192.168.1.10 - 192.168.1.20 Found RANGE: 192.168.1.30 - 192.168.1.40 $VAR1 = '192.168.1.30'; $VAR2 = '192.168.1.31'; $VAR3 = '192.168.1.32'; $VAR4 = '192.168.1.33'; $VAR5 = '192.168.1.34'; $VAR6 = '192.168.1.35'; $VAR7 = '192.168.1.36'; $VAR8 = '192.168.1.37'; $VAR9 = '192.168.1.38'; $VAR10 = '192.168.1.39'; $VAR11 = '192.168.1.40';

Thanks

Replies are listed 'Best First'.
Re: Using map to populate array in for loop
by stevieb (Canon) on Jun 24, 2016 at 14:55 UTC

    You overwrite @ips on each loop. Try changing @ips = map ... to push @ips, map ... (untested).

      You overwrite @ips on each loop. Try changing @ips = map ... to push @ips, map ... (untested).

      Yep, that did it. Simple! Thanks

      Found RANGE: 192.168.1.10 - 192.168.1.20 Found RANGE: 192.168.1.30 - 192.168.1.40 $VAR1 = '192.168.1.10'; $VAR2 = '192.168.1.11'; $VAR3 = '192.168.1.12'; $VAR4 = '192.168.1.13'; $VAR5 = '192.168.1.14'; $VAR6 = '192.168.1.15'; $VAR7 = '192.168.1.16'; $VAR8 = '192.168.1.17'; $VAR9 = '192.168.1.18'; $VAR10 = '192.168.1.19'; $VAR11 = '192.168.1.20'; $VAR12 = '192.168.1.30'; $VAR13 = '192.168.1.31'; $VAR14 = '192.168.1.32'; $VAR15 = '192.168.1.33'; $VAR16 = '192.168.1.34'; $VAR17 = '192.168.1.35'; $VAR18 = '192.168.1.36'; $VAR19 = '192.168.1.37'; $VAR20 = '192.168.1.38'; $VAR21 = '192.168.1.39'; $VAR22 = '192.168.1.40';

        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