Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Using map to populate array in for loop

by stroke (Acolyte)
on Jun 24, 2016 at 15:05 UTC ( [id://1166510]=note: print w/replies, xml ) Need Help??


in reply to Re: Using map to populate array in for loop
in thread Using map to populate array in for loop

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';

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

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1166510]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found