Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-12-01 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?