http://qs321.pair.com?node_id=1093577

Hramyzn has asked for the wisdom of the Perl Monks concerning the following question:

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]; }