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


In reply to Using map to populate array in for loop by stroke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.