[Omaha.pm] Perl-golf -- zero-pad an IP address.

Jay Hannah jay at jays.net
Sun Jan 30 19:29:07 PST 2005


On Jan 28, 2005, at 4:49 PM, Daniel Linder wrote:
> I recently wrote a quick script that took a standard IP address format 
> of
> 12.34.56.78 and reformat it to zero-padded numbers (012.034.056.078).
> Here is  the smallest subroutine I could come up with:
>
> sub zeropad {
>   my $IN = shift;
>   $IN =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
>   return (sprintf("%03d.%03d.%03d.%03d", $1, $2, $3, $4));
> }

sub zeropad {
    return sprintf("%03d.%03d.%03d.%03d", split /\./, $_[0]);
}

Cheers,

j



More information about the Omaha-pm mailing list