On Sat, Dec 28, 2002 at 11:24:36PM -0800, R. Joseph Newton wrote: > I'd say don't bother pack()ing. At the moment, I think the function > pretty much s**ks. use eval() instead. The following doesn't have
pack and unpack are good for what they are good at. If you need their functionality, use them - if not don't. Like regular expressions, they are almost a little language in their own right. I would suggest not using string eval if you can avoid it. It saves up all your problems till runtime, and then adds a few. > that one-liner compactness of the others, but it shows you what is > going on. The bitwise & does a real cute little trick with its > operands, evaluating them in hex context or digit-by-digit (I can't > tell which, and the two may be functionally indistinguishable.) > Therefore I got results like 192 & 255 = 010 [the nine and five match > on the 0 bit, no other digits match any bits with their corresponding > digit in the other number. These are called bitstrings. The operands are converted to bits using your native encoding (probably ASCII), and the operator works on those strings. > That doesn't quite get us there. Here is my approach: > > #!/usr/bin/perl > > my ($IP_String, $MaskString) = split(/ +/, $ARGV[0]); > @IP_Address = ExtractBytes($IP_String); > @Mask = ExtractBytes($MaskString); > @NetworkAddress = AND_MaskArray(4, \@IP_Address, \@Mask, $ARGV); > print > "$NetworkAddress[0]\.$NetworkAddress[1]\.$NetworkAddress[2]\.$NetworkAddress[3]"; > > sub ExtractBytes { > my $DottedString = $_[0]; > @AddressBytes = split (/\./, $DottedString); > return @AddressBytes; > } > > sub AND_MaskArray { > $limit = $_[0]; > my @result; > for (my $i = 0; $i < $limit; $i++) { > $Temp = eval($IP_Address[$i]) & eval($Mask[$i]); I suspect you can remove the evals with 0+$IP_Address[$i] & 0+$Mask[$i] Ugly? Yup. > $result[$i] = $Temp; > } > return @result; > } -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]