Thanks guys, and in the meantime, I've finished the rest of the program. Seems to work perfectly and I'm gonna post it here for everyone to see (maybe it'll be useful to someone.. who knows). Also I'll take a look at what Bethany suggested. But for now, using arithmetic division and modulo worked xD Here's the code if anyone wants it. When running it from a command line type perl than whatever you name the file followed by arguments which go: IP address to subnet, current mask, number of hosts/subnets, "hosts" or "subnets" depending on what you want and in the end, the maximum number of subnets to display. It then displays the new mask, and the subnets including their first and last IP address.

sub dec_to_bin { my $number= $_[0]; my $position= $_[1]; while($number!= 0) { $adress_bin[$position]= $number % 2; $position--; $number= int($number/ 2); } } sub bin_to_dec { my $sum= 0; my $mult= 128; my $i; for $i($_[0] .. $_[1]) { $sum+= $mult* $adress_bin[$i]; $mult/= 2; } return $sum; } sub dec_print { print bin_to_dec(0, 7), ".", bin_to_dec(8, 15), ".", bin_to_dec(16 +, 23), ".", bin_to_dec(24, 31); } sub num_of_bits { my $i= 2; my $number= 2; while($number< $_[0]) { $i++; $number= (2** $i)-2; } return $i; } $adress_dec= $ARGV[0]; $old_mask= $ARGV[1]-1; $num_of= $ARGV[2]; $sub_host= $ARGV[3]; $max_to_show= $ARGV[4]; @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); if($sub_host eq "hosts") { $new_mask= 31- num_of_bits($num_of); } else { $new_mask= $old_mask+ num_of_bits($num_of); } print "New mask: ", $new_mask+1, "\n\n"; $to_show= (2**($new_mask- $old_mask))-2; if($to_show> $max_to_show) { $to_show= $max_to_show; } for $i(1 .. $to_show) { $temp= $i; $i2= $new_mask; while($temp!= 0) { $adress_bin[$i2]= $temp % 2; $i2--; $temp= int($temp/ 2); } for $i2($new_mask+1 .. 31) { $adress_bin[$i2]= 0; } dec_print(); print "\n\t"; $adress_bin[31]= 1; dec_print(); print "\n\t"; for $i2($new_mask+1 .. 31) { $adress_bin[$i2]= 1; } $adress_bin[31]= 0; dec_print(); print "\n"; }

In reply to Re: I need help with the error "Modification of non-creatable array value attempted" by Hramyzn
in thread 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.