RobertJ has asked for the wisdom of the Perl Monks concerning the following question:
I'm using a subroutine I found as part of a program to convert a file with thousands of IP ranges to the minimum number of CIDR blocks. I have tested it and can't find a situation where it doesn't work.
The problems is I would like to understand HOW IT WORKS. Here is the declaration, calling statement and the subroutine:
my (@chunks, @fbits, @lbits); ..... ..... do_chunk(\@chunks, \@fbits, \@lbits); .... .... sub do_chunk { my ($chunks, $fbits, $lbits) = @_; my (@prefix, $idx1, $idx2, $size); # Find common prefix. After that, next bit is 0 for $fbits and 1 for # $lbits is 1. A split a this point guarantees the longest suffix. $idx1 = 0; $idx1++ while ($idx1 <= $#$fbits and $$fbits[$idx1] eq $$lbits[$idx1]); @prefix = @$fbits[0 .. $idx1 - 1]; $idx2 = $#$fbits; $idx2-- while ($idx2 >= $idx1 and $$fbits[$idx2] eq '0' and $$lbits[$idx2] eq +'1'); # Split if $fbits and $lbits disagree on the length of the chunk. if ($idx2 >= $idx1) { $size = $#$fbits - $idx1; do_chunk ($chunks, $fbits, [ @prefix, (split //, '0' . '1' x $size) ]) +; do_chunk ($chunks, [ @prefix, (split //, '1' . '0' x $size) ], $lbits) +; } else { $size = $#$fbits - $idx2; push @$chunks, [ (bits2num( [ @prefix, (split //, '0' x $size) ])), @$ +fbits - $size ]; } }
Now for my questions:
if ($idx2 >= $idx1) { $size = $#$fbits - $idx1; do_chunk ($chunks, $fbits, [ @prefix, (split //, '0' . '1' x $size) ]) +; do_chunk ($chunks, [ @prefix, (split //, '1' . '0' x $size) ], $lbits) +; }
Thanks in advance
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Total Syntax Confusion Plus
by ikegami (Patriarch) on Apr 20, 2009 at 17:51 UTC | |
by RobertJ (Acolyte) on Apr 20, 2009 at 18:12 UTC | |
by ikegami (Patriarch) on Apr 20, 2009 at 18:30 UTC | |
|
Re: Total Syntax Confusion Plus
by pemungkah (Priest) on Apr 21, 2009 at 04:24 UTC | |
by RobertJ (Acolyte) on Apr 21, 2009 at 15:23 UTC |