[EMAIL PROTECTED] wrote: > Greetings, Hello,
> I need to convert the first column of a list of IP numbers to IP > addresses. I created an array of my list but am stumped on how to > convert the numbers. > > File: > 180884576 imstrmcs05 > 180884577 imstrmcs06 > 180884578 imstrmcs07 > 180884579 imstrmcs08 > 180884580 imstrmcs09 > 180884581 imstrmcs10 > > > Script: > # Properly formatting into an array > open(IPLIST, "file") || die "couldn't open the file"; > > while ($x = <IPLIST>) > { > chop $x; > @arr = split /\s+/,$x; > print "@arr\n"; > } > > # Converting IP number to IP address. > foreach $ipaddr { > $ip1 = int ($ipaddr / (256**3)); > $ipaddr %= ($ip1 * 256**3); > $ip2 = int ($ipaddr / (256**2)); > $ipaddr %= ($ip2 * 256**2); > $ip3 = int ($ipaddr /256); > $ip4 = $ipaddr % ($ip3 * 256); > > $realip=$ip1 . "." . $ip2 . "." . $ip3 . "." . $ip4; > print "$realip\n"; > } > > close(IPLIST); Use the Socket module: [EMAIL PROTECTED]:~> echo "180884576 imstrmcs05 180884577 imstrmcs06 180884578 imstrmcs07 180884579 imstrmcs08 180884580 imstrmcs09 180884581 imstrmcs10" | perl -le' use Socket; while ( <> ) { my @arr = split; my $ip = inet_aton $arr[ 0 ]; print inet_ntoa $ip if defined $ip; } ' 10.200.20.96 10.200.20.97 10.200.20.98 10.200.20.99 10.200.20.100 10.200.20.101 John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>