Hello, I've decided to write a program which takes an IP address, its subnet mask and a number of hosts or subnets needed and generates a new subnet mask an prints out all of the subnets available with the new mask. I don't need help with the entire program, since I've already done it in C++ but I'm having some trouble with writing it in Perl.

The problem is that I get an error saying "Modification of non-creatable array value attempted, subscript -33" at line 8. I know that this happens if you try to modify an element of an array under a negative index, but $position is either 31, 23, 15 or 7 (at the start and shouldn't get any lower than 0 if it starts as 7). I don't know what's wrong. Here's all the code I've written until now. It's only supposed to take an IP address in x.x.x.x format and print it as a sequence of 32 bits.

sub dec_to_bin { my $number= $_[0]; my $position= $_[1]; while($number!= 0) { $adress_bin[$position]= $number % 2; $position--; $number/= 2; } } $adress_dec= "192.168.0.0"; # $adress_dec= $ARGV[0]; $old_mask= 16; # old_mask= $ARGV[1]; $num_of= 100; # num_of= $ARGV[2]; $sub_host= "host"; # sub_host= $ARGV[3]; @adress_dec= split(/\./, $adress_dec); for $i (0 .. 31) { $adress_bin[$i]= 0; } dec_to_bin($adress_dec[3], 31); dec_to_bin($adress_dec[2], 23); dec_to_bin($adress_dec[1], 15); dec_to_bin($adress_dec[0], 7); for $i (0 .. 31) { print $adress_bin[$i]; }

In reply to I need help with the error "Modification of non-creatable array value attempted" by Hramyzn

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.