pad ip address with leading zeros in perl script

Warren Block wblock at
Thu Apr 29 20:09:30 PDT 2004

On Thu, 29 Apr 2004, JJB wrote:

> I have perl script written be someone else and I need to fill in the
> each octal with leading zero if needed, so ip address can be sorted
> on.
> This must be an very common requirement.

If you're trying to sort IP addresses that way, maybe.  Some modules let
you get around that much easier, like the inet_aton and inet_ntoa
routines in Socket.

> Is there some standard perl module I can use to perform this task?

Well, Socket, and also look at NetAddr::IP

> An sample of the perl code to accomplish this would be very helpful.

Something I copied from elsewhere:

my @sorted = map inet_ntoa($_), sort map inet_aton($_), @iplist;

Split and join can be used to separate and rejoin the octets, but the
modules are probably better.  Perl questions in general should be sent
to a Perl-specific mailing list or newsgroup.

-Warren Block * Rapid City, South Dakota USA

More information about the freebsd-questions mailing list