[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

It's Perl Golf (note 1) time!

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));
}

Is there any way to get rid of the "$IN" variable?  I tried this but it
didn't work:
sub zeropad {
  shift;
  /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
  return (sprintf("%03d.%03d.%03d.%03d", $1, $2, $3, $4));
}

Dan

Note 1: "Perl Golf" defintion: http://perlgolf.sourceforge.net/

- - - - -
"I do not fear computer,
I fear the lack of them."
 -- Isaac Asimov
GPG fingerprint:9EE8 ABAE 10D3 0B55 C536  E17A 3620 4DCA A533 19BF

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFB+sFvNiBNyqUzGb8RAhlQAKCKrlm4C1fozXImOo26+40Q51bovgCdHOoH
Lm9godPyEJjTjQi5XGmjMhw=
=0zGB
-----END PGP SIGNATURE-----