Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
"My difficulty is I don't really understand well enough the code.."

Perhaps this demo program will help you

use strict; use Socket; my $len = '/29'; print "IP IPN Mask Subnet\n"; for my $i (1...31){ print_subnet('192.168.1.'.$i ,$len); } sub print_subnet { my ( $ip, $len ) = @_; my $ipn = unpack "N", Socket::inet_aton $ip; $len =~ s/^\///; my $maskn = 0xFFFFFFFF << ( 32 - $len ); my $subnet_ipn = $ipn & $maskn; my $subnet_ip = Socket::inet_ntoa pack "N", $subnet_ipn; printf "%-20s %s %x %s\n","$ip/$len",$ipn,$maskn,$subnet_ip; }
It should produce a table like this. Change len values to see effect.
IP IPN Mask Subnet 3232235777 3fffffffc 3232235778 3fffffffc 3232235779 3fffffffc 3232235780 3fffffffc 3232235781 3fffffffc 3232235782 3fffffffc 3232235783 3fffffffc 3232235784 3fffffffc 3232235785 3fffffffc etc

You should be able to add the first comment like this

$subnets{"$ip,$len"}{ipn} = $ipn; $subnets{"$ip,$len"}{maskn} = $maskn; if ( $subnets{"$ip,$len"}{'comment'} eq '' ){ $subnets{"$ip,$len"}{'comment'} = $comment; }

Note : If you did this the comment would be erased by a later blank record

$subnets{"$ip,$len"} = { ipn => $ipn, maskn => $maskn };

To print it out use this

print "subnet,prefix,name\n"; print "$_,$subnet{$_}{'comment'}\n" for sort { $subnets{$a}{ipn} <=> $subnets{$b}{ipn} };

In reply to Re: How to include a variable in output for derived subnets by poj
in thread How to include a variable in output for derived subnets by stroke

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2016-10-28 00:41 GMT
    Find Nodes?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?

      Results (373 votes). Check out past polls.